gpt4 book ai didi

java - 为什么 Long equals 在相同值时返回 false?

转载 作者:行者123 更新时间:2023-11-29 10:09:59 26 4
gpt4 key购买 nike

运行以下代码,我希望得到 true 结果,但我得到的输出是 false

Long value = new Long(0);
System.out.println(value.equals(0));

为什么Longequals比较返回false

最佳答案

Long.equals仅当参数也是 Long 时才返回 true

javadoc 说:

Compares this object to the specified object. The result is true if and only if the argument is not null and is a Long object that contains the same long value as this object.

事实上,下面的代码得到 true 作为输出。

Long value = new Long(0);
System.out.println(value.equals(new Long(0)));
System.out.println(value.equals((long) 0));
System.out.println(value.equals(0L);

关于java - 为什么 Long equals 在相同值时返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43583927/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com