1. What is a packaging class
A new class generated by encapsulating the basic data type corresponding to a package ----> packaging class
Int,byte.....--->Basic data type
Wrapper class ---> reference data type
1. Correspondence of basic data
basic data type | Corresponding wrapper class | inheritance relationship |
byte | Byte | --->Number--->Object |
short | Short | --->Number--->Objec |
int | Integer | --->Number--->Objec |
long | Long | --->Number--->Objec |
float | Float | --->Number--->Objec |
double | Double | --->Number--->Objec |
char | Character | Object |
boolean | Boolean | Object |
2, the reason for the existence of packaging
On the basis of basic data types, the reasons why we create basic classes:
(1) java language: object-oriented language, best at operating various classes.
(2) I used to learn to load data ---" array, int[] String[] double[] Student[]
The ---" collection of loaded data that you will learn in the future has a feature that only data of reference data type can be loaded;
2. Integer class (int)
The wrapper class is the encapsulation of the basic data type: the int type encapsulation produces an Integer
The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a field of type int.
1. Class inheritance relationship:
2. Implemented interface:
3. This class is modified by final, then this class cannot have subclasses and cannot be inherited:
4. Code example (attribute)
Determine the maximum (minimum) value that an Integer can enter:
{ public static void main(String[] args) { //Attributes: System.out.println(Integer.MAX_VALUE); System.out.println(Integer.MIN_VALUE); //The principle of "extremes must be reversed" System.out.println(Integer.MAX_VALUE+1); System.out.println(Integer.MIN_VALUE-1); } }
Output result:
5. Constructor (found no null parameter constructor)
(1) Use int as a constructor parameter:
The underlying method outputs the int type data you input
(2) Use String type data as the constructor parameter:
First, you pass in a string, and the underlying method will determine whether the string you input can be converted to int type;
<1> can be transferred out: direct output
<2> Unable to transfer out: An exception occurred (thrown by the underlying layer)
6. Unique to packaging: automatic packing (automatic unpacking)
Convert primitive data types to wrapper classes
{ public static void main(String[] args) { //Autoboxing: int ----->Integer Integer i = 12; System.out.println(i); //Automatic unboxing: Integer----->int Integer i2 = new Integer(12); int num = i2; System.out.println(num); } }
7. Common methods:
(Main) The bottom layer of the Value of method:
compareTo:
Returns only three values (1, 0, -1)
equals:
Integer rewrites the equals method in Object, and compares the value of the value encapsulated by the underlying layer.
==:
Because == compares the addresses of two objects
Autoboxing:
If the autoboxed value is between -128~127, then the comparison is the specific value, otherwise, the comparison is the address of the object intValue():
The effect will be Intger--->int
parseInt(String s) :
String--->int:
toString() :
Will Integer--->String
Method code display:
public class Test04 { public static void main(String[] args) { //compareTo: only returns three values Integer i1 = new Integer(16); Integer i2 = new Integer(12); System.out.println(i1.compareTo(i2)); //equals:Integer overrides the equals method in Object, and compares the value of the value encapsulated by the underlying layer. //Integer objects are objects created with the new keyword: Integer i3 = new Integer(12); Integer i4 = new Integer(12); System.out.println(i3 == i4);//false because == compares the addresses of two objects //System.out.println(i3.equals(i4)); boolean flag = i3.equals(i4); System.out.println(flag); //Integer objects are done with autoboxing: Integer i5 = 11; Integer i6 = 11; System.out.println(i5.equals(i6)); System.out.println(i5 == i6); /* If the autoboxed value is between -128 and 127, then the specific value is compared Otherwise, the comparison is against the address of the object */ //intValue(): The role will be Intger--->int Integer i7 = 130; int i = i7.intValue(); System.out.println(i); //parseInt(String s) :String--->int: int i8 = Integer.parseInt("12"); System.out.println(i8); //toString() : Convert Integer--->String Integer i9 = 130; System.out.println(i9.toString()); } }