Autounboxing and equality operators(<,>,==)

Let’s guess the output of following program:-

class Test1{
public static void main(String args[])
{
Integer a = new Integer(1024);
Integer b =new Integer(1024);
boolean c = (a<b);
boolean d = (a>b);
System.out.println("a<b"+c);
System.out.println("a>b"+d);
}
}

and another program:-

class Test2{
public static void main(String args[])
{
Integer a = new Integer(1024);
Integer b =new Integer(1024);
boolean c = (a==b);
System.out.println("a==b:"+c);
}
}

Here is the output for first program:-

a<b:false
a>b:false

and Second:-

a==b:false

How could all these value be possible, i mean a is neigther greater/lesser than b not it is equal to b.
Because in Autounboxing these two operators(ie > < and ==) are handled differently. Lets deassemble the first program:-

C:\DOCUME~1\Vivek>javap -c Test1
Compiled from "Test.java"
class Test1 extends java.lang.Object{
Test1();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."":()V
4: return

public static void main(java.lang.String[]);
Code:
0: new #2; //class java/lang/Integer
3: dup
4: sipush 1024
7: invokespecial #3; //Method java/lang/Integer."":(I)V
10: astore_1
11: new #2; //class java/lang/Integer
14: dup
15: sipush 1024
18: invokespecial #3; //Method java/lang/Integer."":(I)V
21: astore_2
22: aload_1
23: invokevirtual #4; //Method java/lang/Integer.intValue:()I
26: aload_2
27: invokevirtual #4; //Method java/lang/Integer.intValue:()I
30: if_icmpge 37
33: iconst_1
34: goto 38
37: iconst_0
38: istore_3
39: aload_1
40: invokevirtual #4; //Method java/lang/Integer.intValue:()I
43: aload_2
44: invokevirtual #4; //Method java/lang/Integer.intValue:()I
47: if_icmple 54
50: iconst_1
51: goto 55
54: iconst_0
55: istore 4
57: getstatic #5; //Field java/lang/System.out:Ljava/io/PrintStream;
60: new #6; //class java/lang/StringBuilder
63: dup
64: invokespecial #7; //Method java/lang/StringBuilder."":()V
67: ldc #8; //String a
69: invokevirtual #9; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
72: iload_3
73: invokevirtual #10; //Method java/lang/StringBuilder.append:(Z)Ljava/lang/StringBuilder;
76: invokevirtual #11; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
79: invokevirtual #12; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
82: getstatic #5; //Field java/lang/System.out:Ljava/io/PrintStream;
85: new #6; //class java/lang/StringBuilder
88: dup
89: invokespecial #7; //Method java/lang/StringBuilder."":()V
92: ldc #13; //String a>b:
94: invokevirtual #9; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
97: iload 4
99: invokevirtual #10; //Method java/lang/StringBuilder.append:(Z)Ljava/lang/StringBuilder;
102: invokevirtual #11; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
105: invokevirtual #12; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
108: return

}

Here the operator “if_icmpge” pops the top two ints off the stack and compares them.
As a and b are having same value 1024 so it will always be false.
Please check this page.

Decompile second program:-

C:\DOCUME~1\Vivek>javap -c Test2
Compiled from "Test.java"
class Test2 extends java.lang.Object{
Test2();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."":()V
4: return

public static void main(java.lang.String[]);
Code:
0: new #2; //class java/lang/Integer
3: dup
4: sipush 1024
7: invokespecial #3; //Method java/lang/Integer."":(I)V
10: astore_1
11: new #2; //class java/lang/Integer
14: dup
15: sipush 1024
18: invokespecial #3; //Method java/lang/Integer."":(I)V
21: astore_2
22: aload_1
23: aload_2
24: if_acmpne 31
27: iconst_1
28: goto 32
31: iconst_0
32: istore_3
33: getstatic #4; //Field java/lang/System.out:Ljava/io/PrintStream;
36: new #5; //class java/lang/StringBuilder
39: dup
40: invokespecial #6; //Method java/lang/StringBuilder."":()V
43: ldc #7; //String a==b:
45: invokevirtual #8; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
48: iload_3
49: invokevirtual #9; //Method java/lang/StringBuilder.append:(Z)Ljava/lang/StringBuilder;
52: invokevirtual #10; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
55: invokevirtual #11; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
58: return

}

Here the operator “if_acmpne” pops the top two object references off the stack and compares them.
As a and b are having different reference it will always be false.
Please check this page.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s