Packaging and automated case loading/unloading
Wrapper class is the eight basic data types in Java encapsulated into a class, all data types can be easily converted to each other with the corresponding wrapper class to solve the application requires the use of data types, and can not use the basic data types of the situation.
int a = 10; // data of basic type
Integer b = new Integer(10); // data represented by the wrapper class
Characteristics of the packaging category
- All wrapper classes are of type final (cannot derive subclasses);
- After instantiating a wrapper class object, the basic type data stored in the object can no longer be changed;
- Wrapper classes encapsulate a large number of useful methods (e.g., data type conversion, case determination, maximum value, minimum value, etc.);
- All wrapper classes inherit from Number except Character and Boolean, which inherit from Object.
Basic data types and wrapper classes
fundamental data type | Packaging |
---|---|
boolean | Boolean class |
byte | Byte class |
short | Short class |
int | Integer class |
long | Long class |
char | Charactor class |
float | Float class |
double | Double class |
Commonalities in Packaging Classes
- The values contained in the wrapper class do not change (the saved values cannot be modified after the wrapper class is instantiated );
- The wrapper class has two constructors
- Integer(int value) --- Constructor object specifying int value
- Integer(String s) --- constructor object that specifies the value of String
- The wrapper classes all have the xxxValue() method
- int intValue() --- returns the int value of the wrapper class object
- float floatValue() --- returns the float value of the wrapper class object
- There are two ValueOf() methods in each of the wrapper classes
- static Integer valueOf(int i) --- Specifies the Integer instance of the int value
- static Integer valueOf(String s) --- Specifies an instance of Integer with a String value.
Automatic packing and unpacking
Auto-boxing and auto-unboxing are conversion mechanisms between basic data types and wrapper classes (done automatically by the compiler)
Basic data types - autoboxing - > wrapper classes;
Packing class --- Automatic unpacking --- > Basic data types.
Example: Take the int type and Integer type for example
public class Test01 {
public static void main(String[] args) {
// Automatic Crating int -> Integer
int i = 10;
Integer integer1 = i;
(integer1);
//Automatic unpacking Integer -> int
int j = 20;
Integer integer2=j;
int k = integer2;
(j);
}
}
Converting Package Types and String Types to Each Other
Example: Integer and String as an example
public class Test03 {
public static void main(String[] args) {
//Wrapping class Integer -> String
Integer i = 100; //auto-boxing
//Method 1
String str1 = i+"";
// Way 2 Call the toString() method of Integer
String str2 = ();
// way 3 Call static method ()
String str3 = (i);
(str1);
(str2);
(str3);
//String -> wrapper class Integer
String str4 = "123";
// Way 1 Call the parseXXX() method of the wrapper class
Integer i2 = (str4); //Automatic unboxing
// way 2
Integer i3 = new Integer(str4); //constructor
(i2).
(i3).
}
}
Example question 1:
Are the outputs of the following two questions the same? Why?
Object obj1 = true?new Integer(1):new Double(2.0); //Ternary operator (is a whole)
(obj1);
// Run the result
// The ternary operator is a whole, and the highest precision is double, so the result is 1.0
1.0
Object obj2.
if(true){
obj2 = new Integer(1);
}else{
obj2 = new Double(2.0); }else{ obj2 = new Integer(1); }else{
}
(obj2);
//Run the result
//if, else are separate statements, what is outputs what
1
Example 2(Integer class):
public class Test04 {
public static void main(String[] args) {
Integer i1 = new Integer(10);
Integer i2 = new Integer(10); (i1 == i2); // false
(i1 == i2); // false (determine if i1 and i2 point to the same object)
((i2)); // true
Integer j1 = 20; // bottom (20) {-128~127} return object reference directly, otherwise create new object new Integer()
Integer j2 = 20; // bottom (20)
(j1 == j2); // true
Integer k1 = 128; // out of range {-128~127}, create new object new Integer()
Integer k2 = 128; // true
(k1 == k2); // false (determine if k1 and k2 point to the same object)
}
}
Example 3 (Integer).
public class Test05 {
public static void main(String[] args) {
Integer i1 = new Integer(127);
Integer i2 = new Integer(127); (i1 == i2); // false i1 and i2 are the same.
(i1 == i2); // false i1 and i2 are two different objects
Integer i3 = new Integer(128); // false
Integer i4 = new Integer(128); // false i1 and i2 are two different objects.
(i3 == i4); // false i3 and i4 are two different objects
Integer i5 = 127; // bottom(127)
Integer i6 = 127;
(i5 == i6); // true
Integer i7 = 128; // bottom new Integer(128)
Integer i8 = 128; // true
(i7 == i8); // false Same as i3 and i4
Integer i9 = 127; // false Same as i3 and i4
Integer i10 = new Integer(127); // false same as i3 and i4 Integer i9 = 127; // false same as i3 and i4
(i9 == i10); // false
int a1 = 127; Integer b1 = 127; // false
Integer b1 = 127; (a1 == b1); // true
(a1 == b1); // true As long as there are basic datatypes, it is the value that is judged to be equal, whether it is an int or an Integer.
int a2 = 128; Integer b2 = 128; // true
Integer b2 = 128; (a2 == b2); // true
(a2 == b2); // true Same as above
}
}
If there are any mistakes, please criticize and correct them to make progress together.