运行以下代码,我希望得到 true
结果,但我得到的输出是 false
。
Long value = new Long(0);
System.out.println(value.equals(0));
为什么Long
的equals
比较返回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/