What is the difference between Integer and int?

If the interviewer asks the difference between Integer and int: It is estimated that most people will only say two points, Ingeter is a wrapper class of int, the initial value of int is 0, and the initial value of Ingeter is null. But what if the interviewer asks again if Integer i = 1; int ii = 1; i==ii is true or false? It is estimated that some people can't answer, and if you ask others, it is estimated that more people will be confused. So I summarize them in the hope that it will be helpful to everyone.


package com.test;
/**
 *
 * @author Liu Ling
 *
 */
public class TestInteger {

    /**
     * @param args
     */
    public static void main(String[] args) {
        int i = 128;
        Integer i2 = 128;
        Integer i3 = new Integer(128);
        //Integer will be automatically unboxed to int, so it is true
        System.out.println(i == i2);//true
        System.out.println(i == i3);//true
        System.out.println("**************");
        Integer i5 = 127;//java is translated into -> 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
    }

}

Both Integer and int ratios are automatically unboxed (above jdk1.5).

The result of line 22 is true, while the result of line 25 is false, and many people do not move why. In fact, when java compiles Integer i5 = 127, it is translated into -> 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 is like this:



1 public static Integer valueOf(int i) {
2 assert IntegerCache.high >= 127;
3 if (i >= IntegerCache.low && i <= IntegerCache.high)
4 return IntegerCache.cache [i + (-IntegerCache.low)];
5 return new Integer(i);
6 }
 


If you look at the source code, everyone will understand that for numbers between -128 and 127, it will be cached. When Integer i5 = 127, it will be cached. 127 is cached, and the next time you write Integer i6 = 127, it will be taken directly from the cache, and there will be no new. So the result of line 22 is true, while the result of line 25 is false.


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


I summarize the above situation as follows:


 
① In any case, Integer and new Integer will not be equal. It will not go through the unboxing process. The reference of i3 points to the heap, while the reference of i4 points to the memory (constant pool) dedicated to storing him. Their memory addresses are different, so it is false.
  ② Both are non-new Integers. If the number is between -128 and 127, it is true, otherwise it is false.
  When compiling Integer i2 = 128, java is translated into -> Integer i2 = Integer.valueOf(128 ); and the valueOf() function will cache the numbers between -128 and 127.
  ③ Both are new and both are false . ④ The
  ratio of int and integer (whether new or not) is true, because the Integer will be automatically Unbox it to int and then compare


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


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324836054&siteId=291194637