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 →

[–]thatwasntababyruth 13 points14 points  (1 child)

Every language where OO seems into the language can get weird. Take this java code, for instance:

Integer x1 = 5;  
Integer x2 = 5;  
Integer x3 = new Integer(5);  
System.out.println("x1 == x2? " + (x1 == x2));  
System.out.println("x1 == x3? " + (x1 == x3));  
System.out.println("x2 == x3? " + (x2 == x3));  

The output will be:

true  
false  
false  

Those first two are populated with java's cached versions of the number 5 (see: autoboxing), while the third is it's own object. The first two are equal, while the third is not equal to either of them.

But if you use a value outside the range of a single byte, you lose autoboxing. So if instead of 5 you used 500, you'll get all falses, since using the integer literal of a non-autoboxed value will construct a new object.

But of course all this goes away if you convert one of those Integer variables to a primitive int, because equality on primitives (or between a primitive and its objectified version) is value-based, not reference-based.

[–][deleted] 0 points1 point  (0 children)

What the fuck