Automatic Packing code in Java

Source: Internet
Author: User

When talking about syntactic sugar in Java virtual machine, I mentioned the following example (not the example in the original article, I changed it myself ):

public class AutoBoxingTest {/** * @param args */public static void main(String[] args) {Integer a = 1;Integer b = 2;Integer c = 127;Integer d = 127;Integer e = 3;Integer f = 3;Long g = 3L;System.out.println(c == d); System.out.println(e == f);System.out.println(e == (a + b));System.out.println(e.equals(a + b));System.out.println(g == (a + b));System.out.println(g.equals(a + b));}}

Output result:

True
True
True
True
True
False
After Jad is decompiled, the result is as follows:

import java.io.PrintStream;public class AutoBoxingTest{    public AutoBoxingTest()    {    //    0    0:aload_0             //    1    1:invokespecial   #1   <Method void Object()>    //    2    4:return              }    public static void main(String args[])    {        Integer integer = Integer.valueOf(1);    //    0    0:iconst_1            //    1    1:invokestatic    #2   <Method Integer Integer.valueOf(int)>    //    2    4:astore_1                Integer integer1 = Integer.valueOf(2);    //    3    5:iconst_2            //    4    6:invokestatic    #2   <Method Integer Integer.valueOf(int)>    //    5    9:astore_2                Integer integer2 = Integer.valueOf(127);    //    6   10:bipush          127    //    7   12:invokestatic    #2   <Method Integer Integer.valueOf(int)>    //    8   15:astore_3                Integer integer3 = Integer.valueOf(127);    //    9   16:bipush          127    //   10   18:invokestatic    #2   <Method Integer Integer.valueOf(int)>    //   11   21:astore          4        Integer integer4 = Integer.valueOf(3);    //   12   23:iconst_3            //   13   24:invokestatic    #2   <Method Integer Integer.valueOf(int)>    //   14   27:astore          5        Integer integer5 = Integer.valueOf(3);    //   15   29:iconst_3            //   16   30:invokestatic    #2   <Method Integer Integer.valueOf(int)>    //   17   33:astore          6        Long long1 = Long.valueOf(3L);    //   18   35:ldc2w           #3   <Long 3L>    //   19   38:invokestatic    #5   <Method Long Long.valueOf(long)>    //   20   41:astore          7        System.out.println(integer2 == integer3);    //   21   43:getstatic       #6   <Field PrintStream System.out>    //   22   46:aload_3             //   23   47:aload           4    //   24   49:if_acmpne       56    //   25   52:iconst_1            //   26   53:goto            57    //   27   56:iconst_0            //   28   57:invokevirtual   #7   <Method void PrintStream.println(boolean)>        System.out.println(integer4 == integer5);    //   29   60:getstatic       #6   <Field PrintStream System.out>    //   30   63:aload           5    //   31   65:aload           6    //   32   67:if_acmpne       74    //   33   70:iconst_1            //   34   71:goto            75    //   35   74:iconst_0            //   36   75:invokevirtual   #7   <Method void PrintStream.println(boolean)>        System.out.println(integer4.intValue() == integer.intValue() + integer1.intValue());    //   37   78:getstatic       #6   <Field PrintStream System.out>    //   38   81:aload           5    //   39   83:invokevirtual   #8   <Method int Integer.intValue()>    //   40   86:aload_1             //   41   87:invokevirtual   #8   <Method int Integer.intValue()>    //   42   90:aload_2             //   43   91:invokevirtual   #8   <Method int Integer.intValue()>    //   44   94:iadd                //   45   95:icmpne          102    //   46   98:iconst_1            //   47   99:goto            103    //   48  102:iconst_0            //   49  103:invokevirtual   #7   <Method void PrintStream.println(boolean)>        System.out.println(integer4.equals(Integer.valueOf(integer.intValue() + integer1.intValue())));    //   50  106:getstatic       #6   <Field PrintStream System.out>    //   51  109:aload           5    //   52  111:aload_1             //   53  112:invokevirtual   #8   <Method int Integer.intValue()>    //   54  115:aload_2             //   55  116:invokevirtual   #8   <Method int Integer.intValue()>    //   56  119:iadd                //   57  120:invokestatic    #2   <Method Integer Integer.valueOf(int)>    //   58  123:invokevirtual   #9   <Method boolean Integer.equals(Object)>    //   59  126:invokevirtual   #7   <Method void PrintStream.println(boolean)>        System.out.println(long1.longValue() == (long)(integer.intValue() + integer1.intValue()));    //   60  129:getstatic       #6   <Field PrintStream System.out>    //   61  132:aload           7    //   62  134:invokevirtual   #10  <Method long Long.longValue()>    //   63  137:aload_1             //   64  138:invokevirtual   #8   <Method int Integer.intValue()>    //   65  141:aload_2             //   66  142:invokevirtual   #8   <Method int Integer.intValue()>    //   67  145:iadd                //   68  146:i2l                 //   69  147:lcmp                //   70  148:ifne            155    //   71  151:iconst_1            //   72  152:goto            156    //   73  155:iconst_0            //   74  156:invokevirtual   #7   <Method void PrintStream.println(boolean)>        System.out.println(long1.equals(Integer.valueOf(integer.intValue() + integer1.intValue())));    //   75  159:getstatic       #6   <Field PrintStream System.out>    //   76  162:aload           7    //   77  164:aload_1             //   78  165:invokevirtual   #8   <Method int Integer.intValue()>    //   79  168:aload_2             //   80  169:invokevirtual   #8   <Method int Integer.intValue()>    //   81  172:iadd                //   82  173:invokestatic    #2   <Method Integer Integer.valueOf(int)>    //   83  176:invokevirtual   #11  <Method boolean Long.equals(Object)>    //   84  179:invokevirtual   #7   <Method void PrintStream.println(boolean)>    //   85  182:return              }}

It can be seen that the final reason for false is that the long type is compared with integer, and different types will not be equal.

In addition, the original text uses 321 instead of 127. In this case, the byte code in Java will use sipush instead of bipush. At this time, = indicates that the result will be false. At present, I don't quite understand why this is the case, hope you can give me some advice.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.