In the past in the packaging class never cared about the concrete implementation of the inside, just feel from the basic type of a sudden to the class object is really magical. Today, referring to Haizi's blog [1], decided to carry out a systematic understanding of the packaging class.
First, what is the packaging class? What happens when a wrapper class and basic type are assigned to each other?
As we all know, Java has 8 basic types, in order to object-oriented needs for these eight types of each design corresponding packaging type. For example, int corresponds to Integer.
If you want to generate an integer with a value of 20, there are two ways to do this:
Integer i=new integer,integer i2= 20;
The first is the normal new object, the second one is the direct assignment. These two kinds of writing will produce different actions, and later.
The second thing to focus on here is that the assignment number "=" will create the corresponding integer object based on the int value, which is the boxing.
So what is unpacking?
int i3=i2;
Use "=" to copy an integer object to the int type variable, which is unboxing.
Second, unpacking and packing is how to achieve
Here's an example.
Public class Main { publicstaticvoid main (string[] args) { integer integer= Ten; int i=integer; }}
Get the following after you decompile
From the results of the anti-compilation can be seen in the boxed integer integer.valueof and unpacking with Integer.intvalue.
So you can summarize the process of packing and unpacking in a sentence:
The boxing process is implemented by calling the wrapper's ValueOf method, and the unboxing process is implemented by invoking the wrapper's Xxxvalue method. (XXX represents the corresponding basic data type).
Not much attention to this detail before, when the string into the int type, preseint and valueof mixed, you have to pay attention to use parseint.
Three, the analysis of packaging class
1> Packaging class can be divided into three kinds
1, packaging class in Character,byte,short, Integer,long is similar, they have a big thing in common is that there is a corresponding cache. Its size and range are as follows:
Class name |
Size |
Range |
Byte |
128 |
0~127 |
Character |
256 |
-128~127 |
Short |
256 |
-128~127 |
Integer |
256 |
-128~127 |
Long |
256 |
-128~127 |
These caches are implemented in the static block, which is created when the wrapper class is loaded, the created objects are saved in the heap, the references are saved in the constant pool, and when a new wrapper class is built by boxing, it is determined that if the value is returned directly within the range, the object corresponding to the wrapper class will be new.
Classes such as:
Character c1= ' A '; Character C2= ' A '; Character C3=new Character (' A '); SYSTEM.OUT.PRINTLN (C1= =C2); System.out.println (C2==c3);
The output is true false, which is the wrapper class object that is boxed, and the object created by new does not.
Character c1=128; Character C2=128; SYSTEM.OUT.PRINTLN (C1==c2);
The output here is false, which is a direct new object outside the range and returns an object reference.
2, float,double These two wrapper classes do not have the corresponding cache, then each boxing will create a new object
Double d1=128.0; Double D2=128.0; System.out.println (D1==d2);
The output is false, and the float type is the same
3, Boolean
The Boolean default creates two Boolean objects of true and false.
Public Static FinalBoolean TRUE =NewBoolean (true); /*** The <code>Boolean</code> object corresponding to the primitive * value <code>false</ Code>. */ Public Static FinalBoolean FALSE =NewBoolean (false); Public StaticBoolean ValueOf (Booleanb) {return(b?true:false); }
So as long as the logic is the same, the corresponding wrapper class object is the same.
2>integer i = new Integer (XXX) and integer i =xxx; the difference between the two methods.
Of course, this topic belongs to a relatively broad category. But the main point must be answered, I summed up the following two points are the difference:
1) The first way does not trigger the automatic boxing process, while the second method will trigger;
2) difference in execution efficiency and resource occupancy. The second approach is more efficient and resource-intensive than the first case in general (note that this is not absolute).
Processing of the 3> compiler
Here the example of Haizi and slightly modified, interested friends directly click on the link below to the original text.
Public Static voidMain (string[] args) {intI=1; Integer a= 1; Integer b= 2; Integer C= 3; Integer D= 3; Integer e= 321; Integer F= 321; Long g= 3L; Long h= 2L; Long k=NewLong (2L); System.out.println (i==a);//1 System.out.println (c==d);//2 System.out.println (E==f);//3 System.out.println (c= = (A +b)); /4 System.out.println (C.equals (a+b)); /5 System.out.println (G= = (A +b)); /6 System.out.println (G.equals (a+b)); /7 System.out.println (G.equals (a+h)); /8 System.out.println (K.equals (a+h)); /9}
When the "= =" on both sides of the wrapper class reference, will directly determine whether it is the same object, that is, the address is the same, if one side is the basic type, one side is the wrapper class object, then the use of Xxxvalue to split the box, the values are compared, and when the operator appears on one side, the same is the case of
When using equals, the expression is first calculated and then boxed, (no type conversions are performed), and the same object is judged.
The above output is:
true //1true//2false //3true //4true //5 true //6false //7true //8false //9
The first one, one side is the basic type, one side is the wrapper object, and the case is split to compare the value true
The second, the third reference above, understand
The fourth right has an operator, and the value of the unboxing comparison is true
The fifth use Equals, now the A and B unpacking the value added to get 3, and then 3 box, and C to compare, because in the range so is true;
The sixth side of the box is split, the numerical comparison
The seventh side is the long side is an integer range
The eighth right converts int to long in the arithmetic operation, and the last boxing is long and within range.
The Nineth k is the object created directly, and certainly not the same object as the other one.
[1] This article reference: http://www.cnblogs.com/dolphin0520/p/3780005.html
Java Boxing and unpacking