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

http://www.cnblogs.com/liuling/archive/2013/05/05/intAndInteger.html
How much do you know about the various comparisons 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.

  首先看代码:

 1 package com.test;
 2 /**
 3  * 
 4  * @author 刘玲
 5  *
 6  */
 7 public class TestInteger {
 8 
 9     /**
10      * @param args
11      */
12     public static void main(String[] args) {
13         int i = 128;
14         Integer i2 = 128;
15         Integer i3 = new Integer(128);
16         //Integer会自动拆箱为int,所以为true
17         System.out.println(i == i2);
18         System.out.println(i == i3);
19         System.out.println("**************");
20         Integer i5 = 127;//java在编译的时候,被翻译成-> Integer i5 = Integer.valueOf(127);
21         Integer i6 = 127;
22         System.out.println(i5 == i6);//true
23         /*Integer i5 = 128;
24         Integer i6 = 128;
25         System.out.println(i5 == i6);//false
26 */        Integer ii5 = new Integer(127);
27         System.out.println(i5 == ii5); //false
28         Integer i7 = new Integer(128);
29         Integer i8 = new Integer(123);
30         System.out.println(i7 == i8);  //false
31     }
32 
33 }

First of all, the output results of lines 17 and 18 are both true, because the 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, 127 will be cached. The next time you write Integer i6 = 127, it will be directly fetched from the cache. , it will not be 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 java compiles Integer i2 = 128, it is translated into -> Integer i2 = Integer.valueOf(128); and the valueOf() function will be between -128 and 127. 3.
Both are new, and both are false . 4. The
ratio of int and integer (regardless of whether new) is true, because Integer will be automatically unboxed into int and then compared.
  If interviewer asks Integer and int The difference: 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.
  First look at the code:

 1 package com.test;
 2 /**
 3  * 
 4  * @author 刘玲
 5  *
 6  */
 7 public class TestInteger {
 8 
 9     /**
10      * @param args
11      */
12     public static void main(String[] args) {
13         int i = 128;
14         Integer i2 = 128;
15         Integer i3 = new Integer(128);
16         //Integer会自动拆箱为int,所以为true
17         System.out.println(i == i2);
18         System.out.println(i == i3);
19         System.out.println("**************");
20         Integer i5 = 127;//java在编译的时候,被翻译成-> Integer i5 = Integer.valueOf(127);
21         Integer i6 = 127;
22         System.out.println(i5 == i6);//true
23         /*Integer i5 = 128;
24         Integer i6 = 128;
25         System.out.println(i5 == i6);//false
26 */        Integer ii5 = new Integer(127);
27         System.out.println(i5 == ii5); //false
28         Integer i7 = new Integer(128);
29         Integer i8 = new Integer(123);
30         System.out.println(i7 == i8);  //false
31     }
32 
33 }

First of all, the output results of lines 17 and 18 are both true, because the 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     }

Looking at the source code, everyone will understand that for numbers between -128 and 127, it will be cached. When Integer i5 = 127, 127 will be cached. The next time you write Integer i6 = 127, it will be taken directly from the cache. , it will not be 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 java compiles Integer i2 = 128, it is translated into -> Integer i2 = Integer.valueOf(128); and the valueOf() function will be between -128 and 127. The number between the two is cached.
③ Both are new, and both are false . ④ The
ratio of int and integer (regardless of whether new) is true, because the Integer will be automatically unboxed into int and then compared.

Guess you like

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