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

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

问题描述

我正在学习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中,开始和开始之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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