编辑:好的,好的,我读错了。我没有将int与Integer进行比较。注意到了。
我的SCJP书上写道:
当==用于将原始类型与包装器对象进行比较时,包装器对象会被解包,然后进行原始类型之间的比较。
所以你可能认为这段代码会打印出true
:
Integer i1 = 1; //if this were int it'd be correct and behave as the book says.
Integer i2 = new Integer(1);
System.out.println(i1 == i2);
但输出结果却是false
。
根据我的书籍,它应该打印true
:
Integer i1 = 1000; //it does print `true` with i1 = 1000, but not i1 = 1, and one of the answers explained why.
Integer i2 = 1000;
System.out.println(i1 != i2);
不是的。它是false
。
怎么回事?
true
。在 ideone 上可以看到。 - NullUserException