Kotlin:1.8.0 的新特性

一、概述

Kotlin 1.8.0版本英语官方文档

Kotlin 1.8.0 中文官方文档

The Kotlin 1.8.0 release is out and here are some of its biggest highlights:

Kotlin 1.8.0发布了,下面是它的一些亮点:

主要演示的其中三个特性,测试结果图如下

二、kotlin-stdlib-jdk7 与 kotlin-stdlib-jdk8 合并进了 kotlin-stdlib

标准库

Kotlin 1.8.0:

稳定了一些函数------Java 与 Kotlin 之间的 TimeUnit 转换、 cbrt()、 Java Optional 扩展函数。

提供可比较且可减去的 TimeMark 预览版。

包含 java.nio.file.path 的实验性扩展函数。

附赠改进了 kotlin-reflect 性能。

2.1 cbrt()函数

cbrt()函数允许您计算双精度或浮点数的实数立方根,它现在是稳定的。

bash 复制代码
fun testcbrt(){
    println("------ cbrt()函数计算双精度或浮点数的实数立方根 结果 ------")
    val num = 27
    val negNum = -num
    println("The cube root of ${num.toDouble()} is:"+ cbrt(num.toDouble()))
    println("The cube root of ${negNum.toDouble()} is:"+ cbrt(negNum.toDouble()))
}

运行结果

2.2 Java 与 Kotlin 之间的 TimeUnit 转换

kotlin中的toTimeUnit()和toDurationUnit()函数。时间现在稳定了。这些功能在Kotlin 1.6.0中作为实验版本引入,提高了Kotlin与Java之间的互操作性。

现在您可以轻松地在Java Java .util.concurrent. timeunit和Kotlin Kotlin .time. durationunit之间进行转换。这些函数仅在JVM上受支持。

bash 复制代码
// For use from Java
fun wait(timeout: Long, unit: TimeUnit) {
    val duration: Duration = timeout.toDuration(unit.toDurationUnit())
    println("java timeout = $timeout $unit 使用 kotlin 转换后 duration = $duration")
}


fun main(){
    testcbrt()
    println("------ Java 与 Kotlin 之间的 TimeUnit 转换 ------")
    wait(1, TimeUnit.HOURS)
    wait(1, TimeUnit.MINUTES)
    wait(60, TimeUnit.MINUTES)
    wait(3, TimeUnit.SECONDS)
    wait(60, TimeUnit.SECONDS)
    wait(1000, TimeUnit.MILLISECONDS)
    wait(1000, TimeUnit.MICROSECONDS)

}

运行结果

2.3 可比较可减去的 TimeMark

在Kotlin 1.8.0之前,如果您想计算多个TimeMark与现在之间的时间差,那么一次只能对一个TimeMark调用elapsedNow()。 这使得比较结果变得困难,因为两个elapsedNow()函数调用不能完全同时执行。

为了解决这个问题,在Kotlin 1.8.0中,您可以从同一时间源中减去和比较timemark。 现在可以创建一个新的TimeMark实例来表示Now,并从中减去其他TimeMark。这样,从这些计算中收集的结果就可以保证彼此是相对的。

bash 复制代码
fun testTimeMark(){
    println("------ 可比较可减去的 TimeMark ------")
    val timeSource = TimeSource.Monotonic
    var mark1 = timeSource.markNow()
    Thread.sleep(500)
    val mark2 = timeSource.markNow()

    repeat(4) { n ->
        val elapsed1 = mark1.elapsedNow()
        val elapsed2 = mark2.elapsedNow()

        // Difference between elapsed1 and elapsed2 can vary depending
        // on how much time passes between the two elapsedNow() calls
        println("Measurement 1.${n + 1}: elapsed1=$elapsed1, " + "elapsed2=$elapsed2, diff=${elapsed1 - elapsed2}")
    }

    println()

    // Since 1.8.0
    repeat(4) { n ->
        val mark3 = timeSource.markNow()
        val elapsed1 = mark3 - mark1
        val elapsed2 = mark3 - mark2

        // Now the elapsed times are calculated relative to mark3,
        // which is a fixed value
        println("Measurement 2.${n + 1}: elapsed1=$elapsed1, " + "elapsed2=$elapsed2, diff=${elapsed1 - elapsed2}")
    }
    // It's also possible to compare time marks with each other
    // This is true, as mark2 was captured later than mark1
    println("mark2 > mark1 : ${mark2 > mark1}")
}

运行结果

三、kt_180.kt文件代码

bash 复制代码
package com.example.test.ktversion

import java.util.concurrent.TimeUnit
import kotlin.math.cbrt
import kotlin.time.Duration
import kotlin.time.TimeSource
import kotlin.time.toDuration
import kotlin.time.toDurationUnit

/*
    https://book.kotlincn.net/text/whatsnew18.html

    标准库
    Kotlin 1.8.0:

    稳定了一些函数------Java 与 Kotlin 之间的 TimeUnit 转换、 cbrt()、 Java Optional 扩展函数。
    提供可比较且可减去的 TimeMark 预览版。
    包含 java.nio.file.path 的实验性扩展函数。
    附赠改进了 kotlin-reflect 性能。
 */
/**
 * cbrt()函数允许您计算双精度或浮点数的实数立方根,它现在是稳定的。
 */
fun testcbrt(){
    println("------ cbrt()函数计算双精度或浮点数的实数立方根 结果 ------")
    val num = 27
    val negNum = -num
    println("The cube root of ${num.toDouble()} is:"+ cbrt(num.toDouble()))
    println("The cube root of ${negNum.toDouble()} is:"+ cbrt(negNum.toDouble()))
}

/*
    Java 与 Kotlin 之间的 TimeUnit 转换
    kotlin中的toTimeUnit()和toDurationUnit()函数。时间现在稳定了。这些功能在Kotlin 1.6.0中作为实验版本引入,提高了Kotlin与Java之间的互操作性。
    现在您可以轻松地在Java Java .util.concurrent. timeunit和Kotlin Kotlin .time. durationunit之间进行转换。这些函数仅在JVM上受支持。
 */
// For use from Java
fun wait(timeout: Long, unit: TimeUnit) {
    val duration: Duration = timeout.toDuration(unit.toDurationUnit())
    println("java timeout = $timeout $unit 使用 kotlin 转换后 duration = $duration")
}

/*
    可比较可减去的 TimeMark
    在Kotlin 1.8.0之前,如果您想计算多个TimeMark与现在之间的时间差,那么一次只能对一个TimeMark调用elapsedNow()。
    这使得比较结果变得困难,因为两个elapsedNow()函数调用不能完全同时执行。

    为了解决这个问题,在Kotlin 1.8.0中,您可以从同一时间源中减去和比较timemark。
    现在可以创建一个新的TimeMark实例来表示Now,并从中减去其他TimeMark。这样,从这些计算中收集的结果就可以保证彼此是相对的。
 */
fun testTimeMark(){
    println("------ 可比较可减去的 TimeMark ------")
    val timeSource = TimeSource.Monotonic
    var mark1 = timeSource.markNow()
    Thread.sleep(500)
    val mark2 = timeSource.markNow()

    repeat(4) { n ->
        val elapsed1 = mark1.elapsedNow()
        val elapsed2 = mark2.elapsedNow()

        // Difference between elapsed1 and elapsed2 can vary depending
        // on how much time passes between the two elapsedNow() calls
        println("Measurement 1.${n + 1}: elapsed1=$elapsed1, " + "elapsed2=$elapsed2, diff=${elapsed1 - elapsed2}")
    }

    println()

    // Since 1.8.0
    repeat(4) { n ->
        val mark3 = timeSource.markNow()
        val elapsed1 = mark3 - mark1
        val elapsed2 = mark3 - mark2

        // Now the elapsed times are calculated relative to mark3,
        // which is a fixed value
        println("Measurement 2.${n + 1}: elapsed1=$elapsed1, " + "elapsed2=$elapsed2, diff=${elapsed1 - elapsed2}")
    }
    // It's also possible to compare time marks with each other
    // This is true, as mark2 was captured later than mark1
    println("mark2 > mark1 : ${mark2 > mark1}")
}

fun main(){
    testcbrt()
    println("------ Java 与 Kotlin 之间的 TimeUnit 转换 ------")
    wait(1, TimeUnit.HOURS)
    wait(1, TimeUnit.MINUTES)
    wait(60, TimeUnit.MINUTES)
    wait(3, TimeUnit.SECONDS)
    wait(60, TimeUnit.SECONDS)
    wait(1000, TimeUnit.MILLISECONDS)
    wait(1000, TimeUnit.MICROSECONDS)
    testTimeMark()
}
相关推荐
千里镜宵烛1 小时前
深入 Lua 环境机制:全局变量的 “容器” 与 “隔离术”
开发语言·junit·lua
QX_hao6 小时前
【Go】--反射(reflect)的使用
开发语言·后端·golang
inferno6 小时前
Maven基础(二)
java·开发语言·maven
我是李武涯7 小时前
从`std::mutex`到`std::lock_guard`与`std::unique_lock`的演进之路
开发语言·c++
史不了8 小时前
静态交叉编译rust程序
开发语言·后端·rust
读研的武8 小时前
DashGo零基础入门 纯Python的管理系统搭建
开发语言·python
Digitally8 小时前
如何用5种实用方法将电脑上的音乐传输到安卓手机
android·智能手机·电脑
Andy8 小时前
Python基础语法4
开发语言·python
但要及时清醒8 小时前
ArrayList和LinkedList
java·开发语言