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()函数允许您计算双精度或浮点数的实数立方根,它现在是稳定的。

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上受支持。

// 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。这样,从这些计算中收集的结果就可以保证彼此是相对的。

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文件代码

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()
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值