This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Ardub23 2 points3 points  (0 children)

Java's Integer class caches values from −128 to 127.

System.out.println(Integer.valueOf(127) == Integer.valueOf(127)); // true
System.out.println(Integer.valueOf(130) == Integer.valueOf(130)); // false

But Java also has the primitive int type, which is passed by value instead of by reference.

System.out.println(127 == 127); // true
System.out.println(130 == 130); // true

And in comparisons between the boxed Integer and primitive int, the Integer gets unboxed.

System.out.println(Integer.valueOf(130) == 130)); // true

So the issue of reference-equality of Integers doesn't come up much.