Before talking about the difference between int and Integer, we must first clarify two issues:
1. Java basic data types and their package classes
2. Java automatic unboxing and Automatic boxing
java automatic unboxing and automatic boxing
- 1, automatic boxing
- Auto boxing is actually the conversion of basic data types to reference data types (object ) Fc4d7#
- 2, automatic unboxing
- automatic unboxing is actually to convert the reference data type to the basic data type
The difference between int and Integer
- analysis
- int
- Integer
- The difference between int and Integer
- The comparison result of int and Integer under the same value
Analysis 9
4daint0d#588int is the basic data type of java.
Integer
Integer inherits the Object class, which is an object type and a wrapper class for int.
The difference between int and Integer
Value storage
int is stored in the stack
Integer The reference of the object is stored in the stack space, and the object data is stored in the heap space.
initialize
int initialized value is 0.
Integer initialized value is null.
Transfer parameters
int is value transfer, the data in the stack is immutable.
Integer objects are passed by reference, the reference is immutable, but the value in the heap space address pointed to by the reference can be changed.
Generic support
Generic does not support int, but supports Integer.
Operation
int can do operations directly, which is a feature of the class.
Integer objects can call methods of this class, but operations cannot be performed before unboxing, and need to be converted to the basic type int.
int is the basic data type, and Integer is the reference data type;
int default value is 0, Integer default value is null;
int type directly stores the value, Integer needs to instantiate the object and point to the address of the object.
Comparison result of int and Integer under the same value
Two variables generated by new, the result is false.
compares the values of int and Integer, if the two values are equal, it is true.
(Note: In comparison, Integer will be automatically unboxed as int type, and then compared.)
Integer variable generated by new and non-new generated Integer variable is compared, and the result is false.
(Note: the value of the Integer variable generated by new is in the heap space, and the value of the Integer variable generated by the non-new is in the constant pool.)
(Note: the Integer variable generated by the non-newIt will first judge whether there is the object in the constant pool, if there is, it will be shared, if not, it will be placed in the constant pool; it is also called the flyweight mode, which will be discussed later. )
Compare two non-new generated Integer objects, and the result is true.
(Note: a prerequisite is required here: the value range is between -128 ~ 127.
relates to a mode of java's automatic boxing and unboxing of int and Integer: Flyweight mode —Flyweight, in order to strengthen the reuse of simple numbers.
In the assignment, the valueOf() method of Integer is actually executed.
When the value is between -128 and 127, java will automatically Pack the box, and then cache the value. If there is the same value next time, it will be directly taken out of the cache and used. The cache is completed by the Integer internal class IntegerCache.
When the value exceeds this range, it will A new object is stored in the heap.
PS: Automatic boxing and unboxing appeared in JDK1.5.
)
Internal class IntegerCache
This can be used Cache simple numbers.
The number of caches can be controlled by -XX: AutoBoxCacheMax =.
When jvm is initialized, the java.lang.Integer.IntegerCache.high property can be set and saved in the private system property.
specifies the value of the low attribute: -128
Look at an example

1: Is a == b? Nonsense, definitely not equal. The addresses of the two new objects are not the same.
2: Is c == d? This is also nonsense, the values of all basic data types must be equal.
3: Is the key question now that e == f? Is g == h?
The answer is: e == f; g != h. Why does this happen? Because Integer g = 130 will be compiled into Integer.valueOf(130) when ava is compiled, which can be seen by decompiling the class file. From the Integer source code, it can be concluded that the Integer.valueOf() method will cache the Integer between the value -128~127, and will not renew one, so e==f; when the value two is greater than 127 or less than- At 128, a new one will be renewed, so g != h.
Integer's valueOf method is as follows:

4: Is c == e, and is i == j?
The answers are all equal. Because when the encapsulation class is compared with the basic data type, Java will automatically unbox it, and then compare whether the values are equal.
In summary, we can draw several conclusions:
1, all are encapsulated classes, and they are all new, and they are definitely not equal. Because the memory address of the object is different.
2. Both are encapsulated classes and are not new. If the value is between -128 and 127, it is equal, otherwise it is not equal.
3. If the encapsulation type is compared with the basic type, as long as the value is equal, it is equal, otherwise it is not equal. Because there is an automatic unboxing operation when the package class and the basic data type are compared.
4, are basic data types, such asIf the values are equal, then they are equal; otherwise, they are not equal.
New class 29.9 yuan limited time discount to 10 yuan