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 21:56

>>18
Reread the thread again.
There is no auto-unboxing.


System.out.println(new Integer(0) == new Integer(0))

>false
== checks that references are the same.

>I don't buy that this is a caching issue,
And Earth is flat.You can believe whatever you want, check Integer.valueOf

    public static Integer valueOf(int i) {
    final int offset = 128;
    if (i >= -128 && i <= 127) { // must cache
        return IntegerCache.cache[i + offset];
    }
        return new Integer(i);
    }


Now check source code of [javap -c]


class a{
public static void main(String [] args){
    Integer aa = 1;
    System.out.println(aa == 1);
}}

Guess what? It uses valueOf. Which uses cache:

public static void main(java.lang.String[]);
  Signature: ([Ljava/lang/String;)V
  Code:
   Stack=3, Locals=2, Args_size=1
   0:    iconst_1
   1:    invokestatic    #2; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;


it's calling Integer.valueOf. Which uses cache.

>sacrificing correctness on this scale for a tiny bit of speed gain is retarded.
it is.

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