Why is 1000==1000 false and 100=100 true in JAVA?

Are you surprised to see this title! Isn't this the result is all the same as false?
If it is the following code: Mu Yong questioned that all objects coming out of the new comparison is that the memory address value is definitely different.

        Integer a = new Integer(100);
        Integer b = new Integer(100);
        System.out.println(a==b);

If it is the following lines of code: the result is indeed not what we thought

        Integer a=100, b=100;
        //结果为true
        System.out.println(a==b);
        Integer c=1000, d=1000;
        //结果为false
        System.out.println(c==d);

Why is the result different?First of all, we have to know the concept of automatic boxing in java.

What is auto-boxing
  • Auto-boxing is to automatically convert primitive values ​​into integer objects, for example, int type into integer objects.
    Because the boxing here is not artificially converted, it is automatic boxing. The
    primitive types byte, short, char, int, long, float, double and boolean correspond to Byte, Short, Character, Integer, Long, Float, Double. , Boolean.
  • IntegerCache internally implements an Integer static constant array. When the class is loaded, the static static block is executed to initialize the Integer objects between -128 and 127, and they are stored in the cache array. Cache is a constant, stored in the method area of ​​java.
        //这行代码实际上自动装箱就会执行下面的代码
        Integer a=100, b=100;
        Integer b = Integer.valueOf(100);

The second is that there is a static inner class IntegerCache in the Integer class , just look at the source code


    public static Integer valueOf(int i) {
    
    
        //判断i是否在-128和127之间,存在则从IntegerCache中获取包装类的实例,否则new一个新实例
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
    //使用亨元模式,来减少对象的创建(亨元设计模式大家有必要了解一下,我认为是最简单的设计模式,也许大家经常在项目中使用,不知道他的名字而已)
    private static class IntegerCache {
    
    
        static final int low = -128;
        static final int high;
        static final Integer cache[];

        //静态方法,类加载的时候进行初始化cache[],静态变量存放在常量池中
        static {
    
    
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
    
    
                try {
    
    
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
    
    
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;
            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);

            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }
        private IntegerCache() {
    
    }
    }

Through the source code analysis, the code block in the original Integer static inner class instantiated the value of the range [-128-127] in advance, which is why the first result is true and the second result is false.

        Integer a=100, b=100;
        //结果为true
        System.out.println(a==b);
        Integer c=1000, d=1000;
        //结果为false
        System.out.println(c==d);

Knowing this, we know why we are confused about the following.

        int i = 127;
        int j = 128;
        Integer a = 102;
        Integer b = 102;
        Integer c = 0;
        Integer n = new Integer(102);
        Integer n1 = new Integer(102);
        //基本数据类型比较 值不相等为false
        System.out.println(i==j);
        //int值只要在-128和127之间的自动装箱对象都从缓存中获取的,所以为true
        System.out.println(a==b);
        //数值计算先进行拆箱转换为int类型比较数值 true
        System.out.println(a==b+c);
        //比较的内存地址值 false
        System.out.println(n==n1);

Guess you like

Origin blog.csdn.net/lirui1212/article/details/108329998