Summary:
There are some basic data types in Java. These basic data type variables cannot call methods, properties, etc. like other objects.
In some cases, some problems arise, and the packaging class is designed to solve them.
Wrapper classes enable these basic data types to have the ability to object
Correspondence between Packaging Class and Base Type
Characteristic:
- Wrapper classes are final modifiers that cannot be inherited
- Number is the parent of the number type
- When a wrapper class is a class property, its default value is Null
Unpacking and packing
Unboxing refers to packaging the underlying data type as an object, as opposed to Unboxing
-
Automatic packing
By assigning the underlying data type directly to the reference variable of the corresponding wrapper class, the system will automatically box the data
Integer a = 10;
-
Auto-unboxing
Integer a = new Integer(10); int b = a;
-
Manual Unboxing
Integer a = new Integer(10); int b = a.intValue();
-
Manual packing/packing
Integer as = new Integer(10);//Packing Integer.valueOf(1);//Packing Float.valueOf(1.1);//Packing .... as.intValue();//Unpacking
Normally we do not need to disassemble the box manually
Auto-unpacking time
1. When the package type variable is assigned directly to the corresponding base type, the system will automatically unpack the box.
2. Auto-unboxing occurs when the real data value of the object is to be accessed, for example, to output the value of the object
3. When the actual value of the packing class is to be mathematically calculated, it is automatically unpacked, such as comparing sizes
It is important to note that when using ==to determine equality, ==either side of the symbol is the underlying data type and the other side is automatically unpacked (no problem), but if both sides are wrapped types, the object references are compared to be the same (which may cause problems).
Integer i1 = new Integer(10); Integer i2 = new Integer(10); System.out.println(i1 == i2); //The result is false
Convert string to wrapper class
There is often a need to convert base data types to string types in development
//We can use the valueof() method provided by the wrapper class to convert strings to wrapper classes of objects System.out.println(Float.valueOf("1.1ff")); System.out.println(Boolean.valueOf("true")); //Conversely, String provides the static method valueOf() any other type converted to string type Object to indicate that all classes, including wrapper classes, are OK System.out.println(String.valueOf('c')); System.out.println(String.valueOf(true));
Object Constant Pool
Java Virtual Opportunity reduces frequent memory operations by default by placing simple literal objects in a constant pool; it is an optimization mechanism;
Test code:
Long s1 = Long.valueOf(127l); Long s2 = 127l; Long s3 = new Long(127l); System.out.println(s1 == s2); //true System.out.println(s1 == s3); //false String st1 = "Constant pool oh oh oh oh"; String st2 = "Constant pool oh oh oh oh"; String st3 = new String("Constant pool oh oh oh oh"); System.out.println(st1 == st2); //true System.out.println(st1 == st3); //false Boolean b1 = true; Boolean b2 = true; Boolean b3 = new Boolean(true); System.out.println(b1 == b2); //true System.out.println(b1 == b3); //false Character c1 = 'a'; Character c2 = 'a'; Character c3 = new Character('a'); System.out.println(c1 == c2); // true System.out.println(c1 == c3); // false Float f1 = 1.0f; Float f2 = 1.0f; Float f3 = new Float(1.0f); System.out.println(f1 == f2);//flase System.out.println(f1 == f3);//flase Double d1 = 1d; Double d2 = 1d; Double d3 = new Double(1d); System.out.println(d1==d2);//flase System.out.println(d1==d3);//flase
- All literals of type String enter the constant pool
- Byte Short Integer Long four types literally enter the constant pool when the object is greater than or equal to -128 and less than or equal to 127
- Character type cannot be negative, object will enter constant pool when literal is greater than or equal to 0 and less than or equal to 127
- Both literals of the Boolean type enter the constant pool
- Float and Double types do not enter the constant pool
Emphasize:
Literal means that the actual value is specified directly during compilation, with very clear data
This means that when we use the new keyword instead of the literal, the object will go directly into the heap and not into the constant pool. See the following example:
Integer a1 = new Integer(10); Integer a2 = new Integer(10); System.out.println(a1 == a2); //false
Once new occurs, new memory must be created and constant pools cannot be used. It is therefore recommended that you avoid using new as much literal as possible in your code.
Supplementary Notes:
These wrapper classes, as well as the static method valueOf() provided by the String class, access the constant pool first rather than immediately new, so you can use it with either the literal or valueOf methods.
Be careful:
The underlying data type does not need to be cached in a constant pool because there is no reference to the underlying data type. In other words, an actual value is stored on the stack with the method.