在 Kotlin 中,start 和 first 有什么区别? [英] In Kotlin, what's the difference between start and first?

查看:25
本文介绍了在 Kotlin 中,start 和 first 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Kotlin,但我似乎无法找到简单问题的直接答案.我想它太新了,还没有人有机会提出明显的问题.就这样吧.

I'm learning Kotlin, but I can't seem to find straight answers to simple questions. I presume that it's so new, no one has had a chance to ask the obvious questions yet. So here it goes.

当我想获得一个范围内的最小项目时,我输入:

When I want to get the smallest item in a range, I type:

range.start

但我收到警告,可以用未装箱的 first 替换".不确定 unboxed 是什么意思——甚至无法猜测.但是当我使用这个命令时:

But I get the warning, "Could be replaced with unboxed first". Not sure what unboxed means--can't even guess. But when I use this command:

range.first

警告消失.这里发生了什么事?我还应该担心吗?为什么 Kotlin 既有 start 又有 first?

the warning goes away. What's happening here? Should I even be concerned? Why does Kotlin have both a start and a first?

推荐答案

装箱和拆箱是指将原始值包装在一个类中,这样它就可以与泛型类和函​​数一起使用,也可以用作可空值.在 Java 中,这更加透明,因为每种类型变量的原始版本和装箱版本都有不同的名称(即 intInteger),而在 Kotlin 中这不是很明显.如果您的变量可以为空,例如 Int?,它总是被装箱,但如果它是不可为空的,它只有在传递给通用函数或请求可空版本时才会被装箱.所以 boxing 作为动词是指变量在传递给需要装箱版本的东西时被包装在一个类中.

Boxing and unboxing refers to wrapping a primitive value in a class so it can be used with generic classes and functions or as a nullable. In Java, this is more transparent because the primitive and boxed versions of each type of variable have different names (i.e. int and Integer), whereas in Kotlin this is not very obvious. If your variable is nullable, like Int?, it is always boxed, but if it's non-nullable, it's only boxed if it's passed to a function that's generic or requests a nullable version. So boxing as a verb refers to the variable getting wrapped in a class at the moment it is passed to something that requires a boxed version.

有一个用于通用范围的接口,称为 ClosedRange.当您使用整数范围时,您正在使用一个名为 IntRange 的类,该类也实现了 ClosedRange<Int>.

There is an interface for a generic range called ClosedRange. When you are working with integer ranges, you are using a class called IntRange that also implements ClosedRange<Int>.

当您使用像 start 这样的通用接口的属性时,JVM 必须对您的 Int 值进行装箱和拆箱.这是因为泛型不能与非装箱原语一起使用.对原语进行装箱和拆箱需要少量的运行时开销.

When you use the properties of the generic interface like start, the JVM has to box and unbox your Int value. This is because generics cannot be used with non-boxed primitives. There is a small amount of runtime overhead to box and unbox the primitive.

实际的类 IntRange 将范围的开始和结束的值存储为基元,因此如果您直接使用 first 访问它们,则会绕过发生的装箱如果您通过通用接口属性,则可以获得少量性能提升.

The actual class IntRange stores the values for the start and end of the range as primitives, so if you access them directly with first, you bypass the boxing that occurs if you go through the generic interface property, for a small performance gain.

在绝大多数情况下,性能差异无论如何都可以忽略不计,但默认代码检查建议您使用性能更高的方式.

In the vast majority of cases, the performance difference will be negligible anyway, but the default code inspection recommends you to use the more performant way.

这篇关于在 Kotlin 中,start 和 first 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆