===运算符在Kotlin中做什么? [英] What does the === operator do in Kotlin?

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

问题描述

运算符===在Kotlin中做什么?它是如何工作的?我们可以检查引用是否相等吗?

What does operator === do in Kotlin? How does it work? Can we check reference equality?

val a: Int = 10000
print(a === a) // Prints 'true'
val boxedA: Int? = a
val anotherBoxedA: Int? = a
print(boxedA === anotherBoxedA) // !!!Prints 'false'!!!

但以防万一:

var a : Int = 1000
var b : Int = 1000
println(a === b) // print 'true' !!!

val a: Int = 1000val b: Int = 1000不在-128..127范围内,但===仍然为true,或者在某些情况下编译器知道可以将其取为一个值?

val a: Int = 1000 and val b: Int = 1000 is not in range -128..127, but still === is true or compiler in some cases understand that it can be taken one value?

推荐答案

根据记录,它表示参考平等:

引用相等性由===操作(及其否定的对等物==)检查.当且仅当a和b指向同一个对象时,a === b的计算结果为true.

Referential equality is checked by the === operation (and its negated counterpart !==). a === b evaluates to true if and only if a and b point to the same object.

引用相等意味着两个引用指向同一对象.每个实例:

Referential equality means that two references point to the same object. Per instance:

fun main(args: Array<String>) {
    val number1 = Integer(10) // create new instance
    val number2 = Integer(10) // create new instance
    val number3 = number1

    // check if number1 and number2 are Structural equality
    println(number1 == number2) // prints true

    // check if number1 and number2 points to the same object
    // in other words, checks for Referential equality
    println(number1 === number2) // prints false

    // check if number1 and number3 points to the same object
    println(number1 === number3) // prints true
}

将此与下面的Java代码进行比较:

Compare this to the Java code below:

public static void main(String[] args) {
    Integer number1 = new Integer(10); // create new instance
    Integer number2 = new Integer(10); // create new instance
    Integer number3 = number1;

    // check if number1 and number2 are Structural equality
    System.out.println(number1.equals(number2)); // prints true

    // check if number1 and number2 points to the same object
    // in other words, checks for Referential equality
    System.out.println(number1 == number2); // prints false

    // check if number1 and number3 points to the same object
    System.out.println(number1 == number3); // prints true
}

您的示例:

此外,如此处记录的所述,数字装箱不保留身份".因此,boxedA将具有一个标识,而anotherBoxedA将具有另一个标识.两者都有结构上的平等,但没有参照上的平等.

Your example:

Also, as documented here, "boxing of numbers does not preserve identity". So, boxedA will have one identity, but anotherBoxedA will have another one. Both have structural equality, but not referential equality.

但是第二个为什么起作用?因为Kotlin Int类型与Java int类型相对应.在第二个示例中比较的两个变量是原始类型值,而不是对象.因此,对于他们来说,引用相等性与常规相等性完全相同.

But why the second one works? Because the Kotlin Int type corresponds to the Java int type. The two variables compared in the second example are primitive type values, not objects. Therefore, for them the reference equality is exactly the same as regular equality.

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

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