What do you know about the comparison between integer and Int?

Source: Internet
Author: User

If the interviewer asks the difference between integer and INT: it is estimated that most people will only talk about two points. ingeter is the int packaging class, the initial value of int is 0, and the initial value of ingeter is null. But if the interviewer asks whether integer I = 1; int II = 1; I = II is true or false? It is estimated that some people cannot answer the question. If you ask another question, it is estimated that more people will be confused. So I have summarized them and hope to help you.

First, check the Code:

1 package COM. test; 2/** 3*4 * @ author Liu Ling 5*6 */7 public class testinteger {8 9/** 10 * @ Param args11 */12 public static void main (string [] ARGs) {13 int I = 128; 14 integer I2 = 128; 15 integer I3 = new INTEGER (128); 16 // integer is automatically split into int, so it is true17 system. out. println (I = I2); 18 system. out. println (I = I3); 19 system. out. println ("***************"); 20 integer I5 = 127; // during Java compilation, translated into-> integer I5 = integer. valueof (127); 21 integer I6 = 127; 22 system. out. println (I5 = I6); // true23/* integer I5 = 128; 24 integer I6 = 128; 25 system. out. println (I5 = I6); // false26 */Integer ii5 = new INTEGER (127); 27 system. out. println (I5 = ii5); // false28 integer i7 = new INTEGER (128); 29 integer i8 = new INTEGER (123); 30 system. out. println (i7 = i8); // false31} 32 33}

First, the output results of rows 17 and 18 are true, because the integer-to-int ratio is automatically split (jdk1.5 or above ).

The result of 22 rows is true, while that of 25 rows is false. 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 and you will understand it. The valueof function type of JDK source code is as follows:

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, you will understand that the number between-128 and 127 will be cached. When integer I5 = 127, 127 will be cached. When integer I6 = 127 is written next time, it will be retrieved directly from the cache and will not be new. Therefore, the result of 22 rows is true, while 25 rows are false.

For 27 rows and 30 rows, the value is false because the object is different.

My summary of the above situation is as follows:

① In any case, integer and new integer are not equal. It will not go through the unpacking process. The reference of I3 points to the heap, while I4 points to the memory that stores it (constant pool). Their memory addresses are different, so it is false.
② Both are non-New integers. If the number is between-128 and 127, the value is true; otherwise, the value is false.
When compiling integer I2 = 128, Java is translated into-> integer I2 = integer. valueof (128); while the valueof () function caches the number between-128 and 127.
③ Both are new and both are false.
④ Int and INTEGER (no matter new) ratios are true, because integer is automatically split into int and then compared

 

If you think there is anything wrong with it, please kindly advise.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.