• java比较相等符


    public class Test1 {
    
        /**
         * @param args
         */
        public static void main(String[] args){
            int a = 1000, b = 1000;
            System.out.println(a == b);
    
            Integer c = 1000, d = 1000;
            System.out.println(c==d);
    
            Integer e = 100, f = 100;
            System.out.println(e == f);         
        }
    }
    
    运行结果:
    
    true
    
    false
    
    true
    
    结果分析:
    
    查看Test1.class文件如下:
    
    public class Test1 extends java.lang.Object{
    public Test1();
      Code:
       0:   aload_0
       1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
       4:   return
    
    public static void main(java.lang.String[]);
      Code:
       0:   sipush  1000
       3:   istore_1
       4:   sipush  1000
       7:   istore_2
       8:   getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
       11:  iload_1
       12:  iload_2
       13:  if_icmpne       20
       16:  iconst_1
       17:  goto    21
       20:  iconst_0
       21:  invokevirtual   #3; //Method java/io/PrintStream.println:(Z)V
       24:  sipush  1000
       27:  invokestatic    #4; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       30:  astore_3
       31:  sipush  1000
       34:  invokestatic    #4; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       37:  astore  4
       39:  getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
       42:  aload_3
       43:  aload   4
       45:  if_acmpne       52
       48:  iconst_1
       49:  goto    53
       52:  iconst_0
       53:  invokevirtual   #3; //Method java/io/PrintStream.println:(Z)V
       56:  bipush  100
       58:  invokestatic    #4; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       61:  astore  5
       63:  bipush  100
       65:  invokestatic    #4; //Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
       68:  astore  6
       70:  getstatic       #2; //Field java/lang/System.out:Ljava/io/PrintStream;
       73:  aload   5
       75:  aload   6
       77:  if_acmpne       84
       80:  iconst_1
       81:  goto    85
       84:  iconst_0
       85:  invokevirtual   #3; //Method java/io/PrintStream.println:(Z)V
       88:  return
    }
    
    由上述字节码我们可以看出 int a = 1000, b = 1000;System.out.println(a == b);执行的是if_icmpne命令,比较的是2个int值是否相等。在此返回为true。
    
    而 Integer c = 1000, d = 1000;System.out.println(c==d);执行的是if_acmpne命令,比较的是对象引用地址。查看jdk源码
    
        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);
        }
    
    此时的值为1000,不在-128~127范围内。所以变量c和d是2个不同的对象。因此System.out.println(c==d);返回结果为false。
    
    而 Integer e = 100, f = 100;System.out.println(e == f);  用的是Integer缓存中的同一对象,因此返回结果为true。

    今天又是学习了。。。以前没有注意这个问题

    转载与:http://blog.csdn.net/jeamking/article/details/6883623

  • 相关阅读:
    codeAnalyze_函数赋值给另一个函数的形参
    js_new关键字创建对象的五个步骤
    codeRecord_bind
    js_活动对象与变量对象的区别
    将linux的随机ip固定为设置的固定ip
    Springcloud总结
    Jackson的使用
    Lucene的初步了解和学习
    Shiro安全框架
    关于xpath中的tbody
  • 原文地址:https://www.cnblogs.com/huzi007/p/4171146.html
Copyright © 2020-2023  润新知