- What is automatic packing unpacking
Automatic boxing (autoboxing), unpacking (unboxing) of the basic data type is a feature that has been available since J2SE 5.0.
Generally we want to create an object instance of a class, we do this:
Class A = new Class (parameter);
When we create an integer object, we can do this:
Integer i = 100; (Note: not int i = 100;)
In fact, the system executes for us when executing the above code: Integer i = integer.valueof (100); (Thanks @ Black bread and @MayDayIT reminder)
This is the automatic boxing feature of the basic data type.
- The difference between a basic data type and an object
The base data type is not an object, which is a variable, constant, defined using an int, double, boolean, and so on.
There is no callable method for the base data type.
Eg:int t = 1; t. There is no way to drip behind.
Integer t =1; t. There are a number of ways that you can invoke it later.
- When to automatically pack
For example: Integer i = 100;
The equivalent compiler automatically compiles the following syntax for you: Integer i = integer.valueof (100);
- When to automatically remove the box
Auto-unpacking (unboxing), that is, the basic data in the object is automatically removed from the object. Automatic unpacking can be implemented as follows:
1 Integer i = 10; Packing
2 int t = i; Unpacking, actually executing an int t = I.intvalue ();
It is also possible to disassemble the container while the operation is in progress.
1 Integer i = 10;
2 System.out.println (i++);
- Automatic boxing of integers
The number outside the -128~127
Integer i1 = 200;
Integer i2 = 200;
System.out.println ("I1==i2:" + (I1==I2));
The number within the -128~127
Integer i3 = 100;
Integer i4 = 100;
System.out.println ("I3==i4:" + (I3==I4));
The result of the output is:
I1==i2:false
I3==i4:true
Description
Equals () compares the values (contents) of two objects.
"= =" compares the two-object reference (memory address) with the same, and is used to compare the values of variables of two base data types for equality.
As mentioned earlier, the automatic boxing of int, is the system executes the integer.valueof (int i), first look at the source code of Integer.java:
123456 |
public
static
Integer valueOf(
int
i) {
if
(i >= -
128
&& i <= IntegerCache.high)
// 没有设置的话,IngegerCache.high 默认是127
return
IntegerCache.cache[i +
128
];
else
return
new
Integer(i);
}
|
For values between –128 to 127 (by default, 127), integer.valueof (int i) returns a cached Integer object (not a new object)
So in the example, i3 and I4 actually point to the same object.
In other values, the execution integer.valueof (int i) returns a new integer object, so in the example, I1 and I2 point to a different object.
Of course, when the auto-boxing function is not used, as in the case of ordinary class objects, consider the following example:
1 integer i3 =new integer (100);
2 integer i4 =new integer (100);
3 System.out.println ("I3==i4:" + (I3==I4));//Display False
(Thanks to the easy name of the reminder O (∩_∩) o~)
Let's look at an example:
1 String str1 = "abc";
2 String str2 = "abc";
3 System.out.println (STR2==STR1); Output is True
4 System.out.println (Str2.equals (str1)); Output is True
5
6 string Str3 =new string ("abc");
7 string STR4 =new string ("abc");
8 System.out.println (STR3==STR4); Output to False
9 System.out.println (Str3.equals (STR4)); Output is True
How do we explain this? I can't seem to see anything. Let's look at an example.
1 String d = "2";
2 String e = "23";
3 E = e.substring (0, 1);
4 System.out.println (E.equals (d)); Output is True
5 System.out.println (E==d); Output to False in the second example, the initial value of E differs from D, so that E and D are each created with an object, (E==d) is false. In the same vein, the STR3 and STR4 in the first example are each new object, and Str1 and str2 refer to the same object. Reference: Http://docs.oracle.com/javase/tutorial/java/data/autoboxing.html http://javarevisited.blogspot.com/2012/07/ Auto-boxing-and-unboxing-in-java-be.html
Java Auto Boxing and unpacking (autoboxing and unboxing)