zoukankan      html  css  js  c++  java
  • autoboxing and unboxing

     Why does 128==128 return false but 127==127 return true

     1 public static void autoboxingUnboxing(){
     2         Integer ta = 127;
     3         Integer tb = 127;
     4         Integer tc = 128;
     5         Integer td = 128;
     6         if(ta == tb) System.out.println("ta equals tb");
     7         if(tc == td) System.out.println("tc equals td");
     8         
     9     }
    10 
    11 
    12 Output:
    13 ta equals tb

    When you compile a number literal in Java and assign it to a Integer (capital I) the compile emits:

    Integer b2 =Integer.valueOf(127)

    This line of code is also generated when you use autoboxing.

    valueOf is implemented such that certain numbers are "pooled", and it returns the same instance for values smaller than 128.

    From the java 1.6 source code, line 621:

     public static Integer valueOf(int i) {
        if(i >= -128 && i <= IntegerCache.high)
            return IntegerCache.cache[i + 128];
        else
            return new Integer(i);
    }

    The value of high can be configured to another value, with the system property.

    -Djava.lang.Integer.IntegerCache.high=999

    If you run your program with that system property, it will output true!

    The obvious conclusion: never rely on two references being identical, always compare them with .equals() method.

    So b2.equals(b3) will print true for all logically equal values of b2,b3.

    private static class IntegerCache {
        private IntegerCache(){}
    
        static final Integer cache[] = new Integer[-(-128) + 127 + 1];
    
        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Integer(i - 128);
        }
    }
    
    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);
    }
    

    As correctly inferred, this caching code was added to support the object identity semantics of autoboxing for values between -128 and 127 (inclusive), as required by the language specification. Since the same object must be returned for integral values in this range, once the autoboxed value for, say, 0 is created, it must be retained until the virtual machine exits. That leaves the issue of whether or not the values in question are created eagerly or lazily and whether or not any container for those values is created eagerly or lazily.

  • 相关阅读:
    航班延误来领钱,信用卡航班延误险最全攻略(2018年版)
    各银行信用卡延误险整理
    酒店web认证802.11x+ROS共享NAT上网
    登机牌,机票,行程单的区别
    ros6.0的包转发图解
    一将成,万骨枯,趣店上市背后的残酷游戏
    异常值检验实战1--风控贷款年龄变量(附python代码)
    outlier异常值检验算法之_箱型图(附python代码)
    sklearn11_函数汇总
    python高级数据可视化Dash2
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/4367540.html
Copyright © 2011-2022 走看看