1. Concept
Let's start with a brief introduction to the conceptual stuff:
The so-called sealing box: The basic type is packaged into its corresponding packaging type;
Unpacking, in turn, is the opposite of converting the package type to its corresponding basic data type.
such as the base type int, the wrapper class after the box is integer.
2. The cache value of the wrapper class, equals and = =
Believe, we must be familiar with equals and = =, the same, the wrapper class also overrides the Equals method, as long as the two wrapper class objects wrapped in the basic type of data is equal, then the two wrapper class objects are considered equal;
Then, look at a simple program and predict the following print results:
Public classTest { Public Static voidMain (string[] args) {System.out.println ("---------------------"); Testbooleanboxing (); System.out.println ("---------------------"); Testbyteboxing (); System.out.println ("---------------------"); Testshortboxing (); System.out.println ("---------------------"); Testintegerboxing (); System.out.println ("---------------------"); Testfloatboxing (); } Public Static voidtestfloatboxing () {Float float1=10F; Float FLOAT2=10F; Float FLOAT3=300F; Float FLOAT4=300F; System.out.println ("Float1.equals (FLOAT2):" +float1.equals (FLOAT2)); System.out.println ("FLOAT1 = = Float2:" + (FLOAT1 = =float2)); System.out.println ("Float3.equals (FLOAT4):" +float3.equals (FLOAT4)); System.out.println ("FLOAT3 = = Float4:" + (FLOAT3 = =float4)); } Public Static voidtestintegerboxing () {Integer int1= 10; Integer Int2= 10; Integer Int3= 300; Integer Int4= 300; System.out.println ("Int1.equals (Int2):" +int1.equals (Int2)); System.out.println ("Int1 = = Int2:" + (Int1 = =int2)); System.out.println ("Int3.equals (INT4):" +int3.equals (INT4)); System.out.println ("Int3 = = Int4:" + (Int3 = =int4)); } Public Static voidtestbyteboxing () {Byte byte1=-128; Byte Byte2=-128; Byte Byte3= 127; Byte byte4= 127; System.out.println ("Byte1.equals (Byte2):" +byte1.equals (Byte2)); System.out.println ("Byte1 = = Byte2:" + (byte1 = =byte2)); System.out.println ("Byte3.equals (BYTE4):" +byte3.equals (byte4)); System.out.println ("Byte3 = = Byte4:" + (Byte3 = =byte4)); } Public Static voidtestshortboxing () {Short Short1=-128; Short Short2=-128; Short Short3= 128; Short Short4= 128; System.out.println ("Short1.equals (SHORT2):" +short1.equals (SHORT2)); System.out.println ("Short1 = = Short2:" + (Short1 = =short2)); System.out.println ("Short3.equals (SHORT4):" +short3.equals (SHORT4)); System.out.println ("Short3 = = Short4:" + (Short3 = =short4)); } Public Static voidtestbooleanboxing () {Boolean bool1=true; Boolean bool2=true; Boolean bool3=true; Boolean Bool4=true; System.out.println ("Bool1.equals (bool2):" +bool1.equals (bool2)); System.out.println ("Bool1 = = Bool2:" + (Bool1 = =bool2)); System.out.println ("Bool3.equals (BOOL4):" +bool3.equals (BOOL4)); System.out.println ("Bool3 = = Bool4:" + (bool3 = =bool4)); }}
Then look at the following results:
---------------------bool1.equals (bool2):trueBool1= = Bool2:truebool3.equals (BOOL4):truebool3= = Bool4:true---------------------byte1.equals (byte2):truebyte1= = Byte2:truebyte3.equals (byte4):trueByte3= = Byte4:true---------------------short1.equals (short2):trueShort1= = Short2:trueshort3.equals (SHORT4):trueShort3= = Short4:false---------------------int1.equals (int2):trueInt1= = Int2:trueint3.equals (int4):trueInt3= = Int4:false---------------------float1.equals (FLOAT2):trueFLOAT1= = Float2:falsefloat3.equals (FLOAT4):trueFLOAT3= = Float4:false
It is not known whether the results of the predictions are the same as the actual results;
Perhaps some people have doubts, different values for the same wrapper class, using the = = operator results are different, this is why?
This is going to start with the immutability of the packaging class. Say the following string first, because the object of the string class cannot be changed since it was created, so it is not dangerous to have multiple references pointing to the same string, so the string constants are placed in the string constant pool at compile time, and can be extracted directly from the string constant pool when it is next used.
The wrapper class, like String, is also immutable, so for some frequently used values, the system provides a cache of wrapper classes (that is, pre-created frequently used wrapper class objects, which can refer to the wrapper class's ValueOf method), which is extracted directly from the cache when needed. Instead of creating a new wrapper class object. This avoids the waste of resources caused by duplicate creation of objects when used repeatedly. The values for these cached wrapper classes are as follows:
- All values of Boolean (True and false);
- The ownership of byte (-128 ~ 127);
- Char value of (0 ~ 127);
- Short Value (-128 ~ 127);
- int value (-128 ~ 127);
- A Long value (-128 ~ 127);
Where floating-point types float and double, the wrapper class is not cached.
As above int type, because 10 is within the cache range of integer, so
Integer int1 = ten= 10;
Two times to 10 of the box is used in the cache in the wrapper value of the integer object (these objects are encapsulated in the Integercache Class), so Int1 and Int2 point to the same object, so whether it is equals or = =, the result of the operation is true;
The value 300 is not in the cached range, so instead of using the object in the cache, you create a new two integer object, so Int3 and int4 use the = = operator with the result of false, and the others are similar to the integer.
If you want to know the detailed cache value, you can view the source of the corresponding cache, such as the value of char can see the Charactercache class, a long value of the viewable Longcache class;
3. Unpacking or sealing the box
The = = and! = in Java can be applied to any type, but what if the two operands of the two-tuple operator are of the basic data type and the other is the wrapper class?
int x = 5= 5; Boolean bool = (x = = y);
If you want to calculate x== y, would you compare the X box to Y for the integer type, or the y to the int type to the x?
Let's look at the following code:
Public Static voidMain (string[] args) {intINT1 = 200; Integer Int2= 200; if(Int1 = =Int2) {System.out.println ("Unpacking Operations"); } Else{System.out.println ("Sealing Operation"); } intInt3 = 10; Integer Int4=NewInteger (10); if(Int3 = =int4) {System.out.println ("Unpacking Operations"); } Else{System.out.println ("Sealing Operation"); } //Mixed Type comparison ShortShort1 = 200; Integer int5= 200; if(Short1 = =int5) {System.out.println ("Unpacking Operations"); } Else{System.out.println ("Sealing Operation"); } }
The returned results are as follows:
Unpacking Operation unpacking Operation
The result has been at a glance. When the base data type is compared with the wrapper type = =, because the memory address is compared, the wrapper class is disassembled into the base data type, and then compared to the two base data types, depending on the result.
4. Sealing and unpacking operations in the method
Look at the following code:
Public classTest { Public Static voidMain (string[] args) {test test=NewTest (); intx = 10; Test.box1 (x); Test.box2 (x); Test.box3 (x); } Public voidBox1 (intx) {System.out.println ("Box1 method for calling the int parameter"); } Public voidBox1 (floatx) {System.out.println ("Box1 method for calling the float parameter"); } Public voidbox1 (Integer x) {System.out.println ("Box1 method for calling the integer parameter"); } Public voidBox2 (floatx) {System.out.println ("Box2 method for calling the float parameter"); } Public voidBox2 (Integer x) {System.out.println ("Box2 method for calling the integer parameter"); } Public voidBox3 (Integer x) {System.out.println ("Box3 method for calling the integer parameter"); }}
After running, look at the following results:
The Box1 method that calls the int parameter calls the Box2 method of the float parameter to invoke the Box3 method of the integer parameter
The result should not be any problem, the problem of overloading I will come out alone to say. What is to be said here is to consider the closure operation (that is, only when the method is not selected to the appropriate parameters, consider the closure operation), mainly in order to be compatible with the previous version. Imagine:
Public void method (float f) {}publicvoid method (Integer i) {}
If you want to call method (7), then the version prior to JDK5, because there is no automatic enclosure, so the float parameter will be called method methods. This is what most people know, if the JDK5 after the rules to prioritize the case, then the program will invoke the integer parameter method, which for most people, is very awkward, the previous rules of the brain to change. Therefore, for the new automatic sealing features, can only be considered after.
Reference from: "Elaborate Java"
Some questions about Java box unpacking