Why == for Integer.valueOf (500) returns false but true for 5?
System.out.println(Integer.valueOf(5) == Integer.valueOf(5));
System.out.println(Integer.valueOf(500) == Integer.valueOf(500));
Output signal
true
false
Why does the first line return true and the second line return false? What a trick here, since both calls are valueOf()
in class Integer
.
+3
OneZero
source
to share
1 answer
There are cached instances of objects with a zero Integer, but not any objects with a higher Integer.
If you haven't noticed before, you are comparing objects, not ints.
+4
NESPowerGlove
source
to share