The difference between Integer and int conversion! Common interview questions

Reprinted https://www.cnblogs.com/liuling/archive/2013/05/05/intAndInteger.html

How much do you know about the comparison between Integer and int?

If the interviewer asks the difference between Integer and int: It is estimated that most people will only say two things. Ingeter is the packaging class of int, the initial value of int is 0, and the initial value of Ingeter is null. But if the interviewer asks Integer i = 1; int ii = 1; i == ii is true or false? It is estimated that some people will not be able to answer. If you ask others, it is estimated that more people will be confused. So I have summarized them and hope to help everyone.

package com.test;

public class TestInteger {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int i = 128;
        Integer i2 = 128;
        Integer i3 = new Integer(128);
        //Integer会自动拆箱为int,所以为true
        System.out.println(i == i2);
        System.out.println(i == i3);
        System.out.println("**************");
        Integer i5 = 127;//java在编译的时候,被翻译成-> Integer i5 = Integer.valueOf(127);
        Integer i6 = 127;
        System.out.println(i5 == i6);//true
        /*Integer i5 = 128;
        Integer i6 = 128;
        System.out.println(i5 == i6);//false
*/        Integer ii5 = new Integer(127);
        System.out.println(i5 == ii5); //false
        Integer i7 = new Integer(128);
        Integer i8 = new Integer(123);
        System.out.println(i7 == i8);  //false
    }

}

First of all, the output results of lines 17 and 18 are both true, because both Integer and int ratios are automatically unboxed (more than jdk1.5).

The result of line 22 is true, and the result of line 25 is false. Many people don't move. In fact, when compiling Integer i5 = 127, java is translated as-> Integer i5 = Integer.valueOf (127); so the key is to look at the valueOf () function. Just look at the source code of the valueOf () function to understand. The valueOf function of the JDK source code looks like this:

 

public static Integer valueOf(int i) {
        assert IntegerCache.high >= 127;
        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[];

        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() {}
    }

 

Looking at the source code, everyone will understand that the number between -128 and 127 will be cached. When Integer i5 = 127, 127 will be cached. Next time when Integer i6 = 127 is written, it will be taken directly from the cache , It will not be new. So the result of line 22 is true, and line 25 is false.

For lines 27 and 30, because the objects are different, it is false.

My summary of the above situation is as follows:

  ①In any case, Integer and new Integer will not be equal. Will not go through the unboxing process, i3's reference points to the heap, and i4 points to his special memory (constant pool), their memory addresses are different, so it is false
  ② Both are non-new Integer, if the number is in- Between 128 and 127, it is true, otherwise it is false.
  When compiling Integer i2 = 128, it is translated into-> Integer i2 = Integer.valueOf (128); and the valueOf () function will be -128 to 127 Cache the number between
  ③ Both are new, both are false
  ④ The ratio of int and integer (whether new or not) is true, because Integer will be automatically unboxed as int and then compared

 

If you think something is wrong, welcome instructions.

 

 

Published 13 original articles · Like 3 · Visits 4996

Guess you like

Origin blog.csdn.net/u010919402/article/details/93672899