Difference between int and Integer; Difference between intInteger

Source: Internet
Author: User

Difference between int and Integer; Difference between intInteger

Difference between int and Integer
1. Integer is the packaging class of int, and int is a basic data type of java.
2. Integer variables must be instantiated before they can be used. int variables do not need
3. Integer is actually a reference to an object. When a new Integer is used, a pointer is actually generated to point to this object, while int is directly used to store data values.
4. The default value of Integer is null, and the default value of int is 0.

Extension:
Comparison between Integer and int
1. Since the Integer variable is actually a reference to an Integer object, the two Integer variables generated through new are never equal (because new generates two objects, the memory address is different ).

Integer I = new Integer (100 );
Integer j = new Integer (100 );
System. out. print (I = j); // false
2. When the Integer and int variables are compared, the result is true as long as the values of the two variables are equal (because the Integer of the packaging class is compared with the int of the basic data type, java will automatically split and wrap it as int, and then compare it. In fact, it will become a comparison between two int variables)

Integer I = new Integer (100 );
Int j = 100;
System. out. print (I = j); // true
3. When the non-new Integer variable is compared with the new Integer () variable, the result is false. (Because the non-new Integer variable points to the object in the java constant pool, while the new Integer () variable points to the object created in the heap, the addresses in the memory are different)

Integer I = new Integer (100 );
Integer j = 100;
System. out. print (I = j); // false
4. For two non-new Integer objects, if the values of the two variables are between-128 and 127, the comparison result is true, if the values of the two variables are not in this interval, the comparison result is false.

Integer I = 100;
Integer j = 100;
System. out. print (I = j); // true
Integer I = 128;
Integer j = 128;
System. out. print (I = j); // false
For the reason of article 4th:
Java translates Integer I = 100 into Integer I = Integer. valueOf (100) when compiling Integer I =; while the definition of valueOf Integer type in java API is as follows:

Public static Integer valueOf (int I ){
Assert IntegerCache. high >=127;
If (I> = IntegerCache. low & I <= IntegerCache. high ){
Return IntegerCache. cache [I + (-IntegerCache. low)];
}
Return new Integer (I );
}
Java caches the numbers between-128 and 127. When Integer I = 127, 127 is cached. When Integer j = 127 is written again, it will be retrieved directly from the cache, and it will not be new

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.