`

Double Integer

 
阅读更多
                   
                    Double d=2.0;
		  Double d1=2.0;
 Double d2=3.0;
 Double d3=1111.0;
		  System.out.println(d==d1);//false
		  
		  Integer i=1;
		  Integer i1=1;
		  Integer i2=new Integer(1111);//
		  System.out.println(i==i1);//true
		  
		 


要搞清为什么这么输出,得看字节码

//    0    0:ldc2w           #2   <Double 2D>
    //    1    3:invokestatic    #4   <Method Double Double.valueOf(double)>
    //    2    6:astore_1        
    //    3    7:ldc2w           #2   <Double 2D>//从常量池中得到Double
    //    4   10:invokestatic    #4   <Method Double Double.valueOf(double)>
//通过Double.valueOf得到了Double对象!所以虽然树值一样!但不是一个对象!
    //    5   13:astore_2        
    //    6   14:ldc2w           #5   <Double 3D>
    //    7   17:invokestatic    #4   <Method Double Double.valueOf(double)>
    //    8   20:astore_3        
    //    9   21:ldc2w           #7   <Double 1111D>
    //   10   24:invokestatic    #4   <Method Double Double.valueOf(double)>
    //   11   27:astore          4
    //   12   29:getstatic       #9   <Field PrintStream System.out>
    //   13   32:aload_1         
    //   14   33:aload_2         
    //   15   34:if_acmpne       41
    //   16   37:iconst_1        
    //   17   38:goto            42
    //   18   41:iconst_0        
    //   19   42:invokevirtual   #10  <Method void PrintStream.println(boolean)>
    //   20   45:iconst_1        
    //   21   46:invokestatic    #11  <Method Integer Integer.valueOf(int)>
    //   22   49:astore          5
    //   23   51:iconst_1        
    //   24   52:invokestatic    #11  <Method Integer Integer.valueOf(int)>
    //   25   55:astore          6
    //   26   57:new             #12  <Class Integer>
    //   27   60:dup             
    //   28   61:sipush          1111
    //   29   64:invokespecial   #13  <Method void Integer(int)>
    //   30   67:astore          7
    //   31   69:getstatic       #9   <Field PrintStream System.out>
    //   32   72:aload           5
    //   33   74:aload           6
    //   34   76:if_acmpne       83
    //   35   79:iconst_1        
    //   36   80:goto            84
    //   37   83:iconst_0        
    //   38   84:invokevirtual   #10  <Method void PrintStream.println(boolean)>
    //   39   87:getstatic       #9   <Field PrintStream System.out>
    //   40   90:aload           7
    //   41   92:aload           6
    //   42   94:if_acmpne       101
    //   43   97:iconst_1        
    //   44   98:goto            102
    //   45  101:iconst_0        
    //   46  102:invokevirtual   #10  <Method void PrintStream.println(boolean)>
    //   47  105:return    

double d4=3.0;

如果class文件中只有这一句
看字节码
//    0    0:ldc2w           #2   <Double 5D>
    //    1    3:dstore_1        
    //    2    4:return        

而Integer对象-127-128是在常量池中!
Integer i=1;
字节码是
  //Integer integer = Integer.valueOf(1);
 //    2    4:iconst_1        
    //    3    5:invokestatic    #4   <Method Integer Integer.valueOf(int)>



当Integer i=200
则会打印出false;
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics