The difference between int and integer

Source: Internet
Author: User

Ingeter is the wrapper class for int, and the initial value of int is null for 0,ingeter.

int is the basic data type
Integer is its wrapper class, note is a class.
The base type is stored in the stack, while the base type wrapper class is stored in the heap.
Why do you want to provide the packaging class???
One is to convert between various types, through the invocation of various methods. Otherwise, you cannot convert directly from a variable.
For example, now int should be converted to string
int a=0;
String result=integer.tostring (a);
In the Java wrapper class, the more useful use is in the conversion of various data types.
I'll write a few demos.

Through the packaging class to achieve conversion
int num=integer.valueof ("12");
int Num2=integer.parseint ("12");

Double num3=double.valueof ("12.2");
Double num4=double.parsedouble ("12.2");
The others are similar. ValueOf and Parsexx through the wrapper of the basic data type to convert the string to XX

String a=string.valueof ("1234");//There can be almost any type in parentheses
String b=string.valueof (TRUE);
String c=new Integer (n). ToString ();//through the wrapper class, ToString () can also
String d=new Double (2.3). ToString ();

Another example. Like I'm going to use generics now.
List<integer> Nums;
Here <> need class. If you use Int. It will be an error.

Example:
 Packagecom.test; Public classTestinteger { Public Static voidMain (string[] args) {inti = 128; Integer i2= 128; Integer i3=NewInteger (128);//integer is automatically unboxing to int, so trueSystem.out.println (i = =i2); System.out.println (i==i3); System.out.println ("**************"); Integer i5= 127;//when Java is compiled, it 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 =NewInteger (127); SYSTEM.OUT.PRINTLN (i5= = Ii5);//falseInteger i7 =NewInteger (128); Integer i8=NewInteger (123); SYSTEM.OUT.PRINTLN (i7= = i8);//false}}

First, 17 rows and 18 rows of output are true because the integer and int ratios are automatically unboxing (jdk1.5 or more).

The result of 22 rows is true, while 25 rows is false and many people do not move why. In fact, when compiling the integer i5 = 127, Java is translated into integer i5 = integer.valueof (127), so the key is to see the valueOf () function. Just look at the valueof () function of the source code will understand. JDK source code of the valueof function, such as:

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

Look at the source code everyone will understand that for the number between 128 to 127 will be cached, Integer i5 = 127, 127 will be cached, the next time you write an Integer I6 = 127, will be directly from the cache, will not be new. So the result of 22 rows is true, while the 25 behavior is false.

For 27 rows and 30 rows, false because the objects are not the same.

My summary of the above situation is as follows:

① Anyway, the integer is not equal to the new integer. Does not go through the unboxing process, the i3 reference points to the heap, and I4 points to his memory (Chang), whose memory address is not the same, so false
② two are non-new integer, if the number is between 128 to 127 is true, otherwise false
Java is translated as Integer i2 = integer.valueof (128) When compiling integer i2 = 128, while the valueOf () function caches the number from 128 to 127
③ two are all new, false.
④int and Integer (regardless of new No) are true because the integer is automatically removed to int and then to the

The difference between int and integer

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.