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:50

I just ran a test.  The essential code is:
int i = -1000;
Integer a, b;
for(; i <= 1000; i++)
{
   a = i;
   b = i;
   if(a == b)
   {
      System.out.println("a == b ("+i+" == "+i+"): " + (a == b));
   }
}


The results? -128 to +127 returned true for comparing Integer a and Integer b.  In other words, this terrible unboxed comparison, where the wrapper class is fed with a primitive rather than a constructor, is true only for unsigned 8-bit int values.  The same might apply for the Float and Double wrapper classes in some way but I am not interested in checking at the moment; maybe later.

Furthermore, there is no auto-unboxing in Java.  WYSIWYG

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