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()
}
相关推荐
Highcharts.js1 小时前
成分占比饼图代码|常用可视化图表开发示例
java·开发语言·仪表盘·highcharts·实战代码·响应式图表
蓝博AI3 小时前
2027基于yolov8飞机蒙皮缺陷检测,支持图像,视频和摄像实时检测,python源码
开发语言·python·yolo
郝学胜-神的一滴5 小时前
游戏设计、原型与开发 26:四种循环
开发语言·数据结构·游戏·游戏引擎·游戏策划
泡海椒6 小时前
Java PDF 报表开发:一页简历的两种版式风格对比与折行避坑
java·开发语言·pdf
2601_962885726 小时前
如何用 Python 计算终极振荡器(Ultimate Oscillator, UOS)?
开发语言·python
benchmark_cc7 小时前
一次获取多只股票实时行情:QuantDash `quotes.get(symbols=[...])` 批量快照
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
geovindu7 小时前
python: PCA
开发语言·后端·python·算法
__万波__8 小时前
嵌入式存储技术--1.Flash物理存储
java·开发语言
Chen_harmony8 小时前
十六、ALV树
开发语言·c++
qq_274475159 小时前
第 28 篇:Promise 全解析:状态、then/catch/finally、静态方法 all/race/allSettled/any
开发语言·javascript