Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

On virtual machines and expected behaviour

Name: Anonymous 2010-05-17 19:44

Ever tried running this on your ENTERPRISE VIRTUAL MACHINE?
Integer a = 1000;
 Integer b = 1000;
 System.out.println("a == b (1000 == 1000): " + (a == b));

 Integer aa = 100;
 Integer bb = 100;
 System.out.println("aa == bb (100 == 100): " + (aa == bb));

Guess what's the output, run it and report back.

Name: Anonymous 2010-05-17 20:53

>>9
The equality operator is crucial in the comparison failing- ergo your wrong.
The point is that all Integer values under a certain bound (128 maybe?) are cached for efficiency when dealing with autoboxing. When creating aa and bb it simply returns the Integer object that was cached on VM startup so the references are identical and the native equality returns true. 1000 is above this bound, so a and b point to two different instantiations of the Integer class- i.e.: we need to use the equals method (as documented in the spec) to test for object equality, not ==.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List