Comparison between integer and INT!

Source: Internet
Author: User

Today I see some differences between integer and Int. Here I will record the following:

Int Is the Basic Data Type and directly stores data. integer is a packaging class and an object. It points to this object with a reference.

In Java, packaging classes are mostly used for conversion of various data types.

For example, when you need to put something in arraylist and hashmap, the built-in types such as int and double cannot be put, because the containers are all loaded with objects, which requires packaging classes.

The initial value of int is 0, and that of integer is null.

 1       Int I = 100. ;  2 Integer I2 = 1, 100 ;  3 Integer I3 = New INTEGER (1, 100 );  4 System. Out. println (I = I2 );//  True  5 System. Out. println (I = I3 ); //  True  6           7 System. Out. println (i2.hashcode ()); //  100  8 System. Out. println (i3.hashcode ()); //  100  9 System. Out. println (I2 = I3 ); // False  10   11 Integer I4 = 127; //  During Java compilation, it is translated into-> integer I4 = integer. valueof (127 );  12 Integer I5 = 127 ;  13 System. Out. println (I4 = I5 ); //  True  14           15 Integer I6 = 128 ;  16 Integer integer i7 = 128 ;  17 System. Out. println (I6 = i7 ); //  False
System. Out. println (i6.equals (i7); // true

All rows 4 and 5 are true, because the integer and INT ratios are automatically split.
Row 3 is false because the objects are different.

13th is true, while 17 is false. Some people may not know why. In fact, when Java compiles integer I4 = 127, it is translated as-> integer I4 = 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       Final   Int Offset = 128 ;  3       If (I >=- 128 & I <= 127 ){ //  Must Cache  4           Return Integercache. cache [I + Offset];  5   }  6           Return  New  INTEGER (I );  7 }

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 above results will appear.

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, 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 will cache the number between-128 and 127, and the number outside will be new.
③ 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

 

 

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.