在下面的代码中,哈希码总是相同的。为什么会这样?
代码:
public class BooleanClass {
public static void main(String[] args) {
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(false);
Boolean b3 = new Boolean(true);
Boolean b4 = new Boolean(false);
Boolean b5 = new Boolean(false);
Boolean b6 = new Boolean(true);
System.out.println(b1.hashCode());
System.out.println(b2.hashCode());
System.out.println(b3.hashCode());
System.out.println(b4.hashCode());
System.out.println(b5.hashCode());
System.out.println(b6.hashCode());
}
}
输出:
1231
1237
1231
1237
1237
1231
总是打印相同的数字 1231
和 1237
。有什么原因吗?
最佳答案
The JavaDoc Boolean.hashCode()
方法说:
Returns the integer
1231
if this object representstrue
; returns the integer1237
if this object representsfalse
.
关于java - 为什么不同 boolean 实例的哈希码总是相同的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22683634/