The introduction of auto- boxing (autoboxing) and Auto-unpacking (unboxing)after JDK1.5, which made a lot of confusion for beginners in Java, was a part of me just now.
First, there are some basic concepts that need to be understood:
1. Ingeter is the wrapper class for int, and the initial value of int is null for 0,ingeter.
2. An integer is a class that declares a variable as an object type (or reference type), an int is a primitive type, a variable declared with an int is a non-object type, which means that the method cannot be invoked on it.
3,"= =" when acting on the object, it compares the value of the object's reference itself (or the object's address is easier to understand), and for the basic type of comparison is the basic type of value.
Then, we need to understand two basic concepts: automatic boxing and automatic unpacking
Integer a=1;//This is an automatic boxing, if not automatically boxed, you need such an integer a=new integer (1) int b=a;//This is an automatic unpacking, if not automatically unpacking, you need this: int b= A.intvalue () so you can see that auto-boxing and auto-unpacking are conversion steps that simplify basic data types and relative objects
Next we will describe the difference between an integer and an int with specific code:
1., INteger x = 10 is not equal to integer y = new Integer (10). Because the two do not experience the unboxing process when compared, the latter refers to the heap, while the former points to the memory ( -128~127 Chang) that is dedicated to storing it, and their memory addresses are different, so false,
public static void Main (string[] args) {Integer i = 10;integer j = new Integer (10); System.out.println (i = = j); False
2, two are not new integers, if the number is between 128 to 127 is true, otherwise false
public static void Main (string[] args) {Integer i = 10;integer j = 10; System.out.println (i = = j); True
public static void Main (string[] args) {Integer i = 128;integer j = 128; System.out.println (i = = j); False
Many people here do not understand why, here need to say why:
when compiling the integer i = 10, Java is translated into Integer i = integer.valueof (ten);
JDK source code of the valueof function, such as:
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); }
for a number between 128 and 127, the cache will be cached, and the integer i = 10 o'clock will cache 10, and the next time you write the Integer j = 10 o'clock, it will be taken directly from the cache and will not be new.
So the result of the first piece of code is true.
Conversely, for a number other than 128 to 127, the last line of the valueof function is referenced to return a new integer object.
When the integer i =128 produces the first integer object, the second integer object is generated when the integer j = 128, and the address of the two objects is different.
3, when both are new Integer () out, the two must be different.
public static void Main (string[] args) {integer i = new Integer (ten); integer j = new Integer (10); System.out.println (i = = j); False
Here is a comparison, that is, the Integer i = 10;integer J = 10 is the same, because at this time I and J get is the cache value of 10 address (reference 2)
4. If the integer type is compared to the int type, the integer type is the same as the int type as long as the values of the two are equal.
public static void Main (string[] args) {Integer i = 128;integer j = new Integer (+); int k = 128; System.out.println (i = = j); FalseSystem.out.println (i = = k); TrueSystem.out.println (j = = k); True
Because Java is a downward transition when the integer type is compared to the int type, the integer type automatically splits into int type, so the two compare values when compared.