💨💨💨------ preface
-
Is there a need to pass in a number when calling the doSome() method. However, the number belongs to the basic data type, and the parameter type of doSome() method is Object, which belongs to the reference type. It can be seen that doSome() method cannot receive the number of basic data type. At this time, a packaging class corresponding to the number can be passed in.
public class IntegerTest01 { public static void main(String[] args) { // Wrap the number 100 into an object through the construction method. Integer myInt = new Integer(100); // Although the doSome() method cannot directly pass 100, it can pass a package type corresponding to 100. doSome(myInt);//100 } public static void doSome(Object obj){ //System.out.println(obj); System.out.println(obj.toString()); } }
💨💨💨------ Packing type name & packing and unpacking
-
8 package type names corresponding to 8 basic data types
- Basic data type ------------------------------------------------- package type
Byte -------------------------------------------------------- java.lang.byte (the parent class is Number)
Short ------------------------------------------------------- java.lang.short (the parent class is Number)
Int ------------------------------------------------------- java.lang.integer (the parent class is Number)
Long -------------------------------------------------------- java.lang.long (the parent class is Number)
Float -------------------------------------------------------- java.lang.float (the parent class is Number)
Double -------------------------------------------------------- java.lang.double (the parent class is Number)
Char -------------------------------------------------------- java.lang.character (the parent class is Object)
Boolean -------------------------------------------------------- java.lang.boolean (the parent class is Object)
- Six of the eight wrapper classes are wrapper classes corresponding to numbers, and their parent class is Number
------Number is an abstract class and cannot instantiate an object
------There are such methods in the Number class:
(these methods are actually available in all subclasses of the digital packaging class. These methods are responsible for unpacking.)- (byte)byteValue() - returns the specified value in byte.
- (abstract double) doubleValue() - returns the specified value as a double.
- (abstract float) floatValue() - returns the specified value as a float.
- (abstract int) intValue() - returns the specified value as int.
- (abstract long) longValue() - returns the specified value as long.
- (short) shortValue() - returns the specified value as a short.
- Basic data type ------------------------------------------------- package type
-
Packing & unpacking
- Basic data type - (converted to) - > reference data type ------ (boxed)
- Bold style
public class IntegerTest02 { public static void main(String[] args) { // This basic data type is wrapped with the construction method to achieve the conversion from basic data type to reference data type. // Basic data type - (converted to) - > reference data type ------ (boxed) Integer i = new Integer(123); // Reference data type -- (convert to) - > basic data type -- (unpacking) int i1 = i.intValue(); System.out.println(i1);//123 } }
-
Automatic packing & automatic unpacking
After Java 5, a new feature, automatic packing and automatic unpacking, is introduced
------Auto packing: the basic data type is automatically converted to packing class
------Automatic unpacking: packaging class is automatically converted to basic data type
——Note: after automatic unpacking, the methods in the Number class can not be usedpublic class IntegerTest02 { public static void main(String[] args) { // 900 is the basic data type // x is the packaging type // Basic data type -- (automatic conversion) -- packing type: automatic packing Integer x = 900;// Equivalent to: integer x = new integer (900). Is x a reference or a memory address of an object saved System.out.println(x);// 900 // x is a wrapper class and does not belong to the basic data type. To operate with the basic data type, you need to convert it first // Starting from java5, the packing class and the basic data type will be unpacked automatically to convert the packing class to the basic data type System.out.println(x+1);// 901 // x is the packaging type // y is the basic data type // Package type -- (automatic conversion) -- basic data type: automatic unpacking int y = x;// Equivalent to: int y = x.intValue System.out.println(y);// 900 } }
-
Extension: integer constant pool, cache mechanism
In java, in order to improve the execution efficiency of the program, all wrapper objects between the Byte value range [- 128127] are created in advance and placed in the "integer constant pool" in the method area. The purpose is to take them directly from the integer constant pool as long as the data in this range does not need a new object
public class IntegerTest03 { public static void main(String[] args) { Integer a = 128; Integer b = 128; System.out.println(a == b);//false // The principle that x==y is true: // The object memory address saved in the x variable and the object memory address saved in the y variable point to the same, which is in the integer constant pool of the method area Integer x = 127; Integer y = 127; System.out.println(x == y);//true } }
💨💨💨------ Integer wrapper class (similar to other wrapper classes)
- Construction method
- Integer(int value)
——Convert int type to Integer wrapper type (int -- > Integer) - Integer(String s)
——Converts a number of type String to an Integer wrapper type (String -- > Integer)
public class IntegerTest04 { public static void main(String[] args) { //Two construction methods of Integer //Pass in an int type parameter constructor Integer i = new Integer(1000); System.out.println(i.intValue()); //Pass in a String type parameter boxing constructor Integer i1 = new Integer("1000"); System.out.println(i1.intValue()); //If the parameter of String type passed in is not a number, but text, can it be wrapped into Integer? //No, the compilation can pass, and NumberFormatException (number formatting exception) is thrown at runtime Integer x = new Integer("written words");//NumberFormatException System.out.println(x.intValue()); } }
- Integer(int value)
- constant
(static int) MAX_VALUE ------ the maximum value of the current data type
(static int) MIN_VALUE ------ the minimum value of the current data type - common method
static int parseInt(String s) - a static method that passes in a String parameter and returns int
Static Integer valueof (int i | String s) - a static method that passes in int or String parameters and returns Integer
static String toBinaryString(int i) - static method: convert decimal to binary string
static String toOctalString(int i) - static method: convert decimal to octal string
static String toHexString(int i) - static method: convert decimal to hexadecimal stringpublic class IntegerTest04 { public static void main(String[] args) { //Key methods //static int parseInt(String s) -- static method. Pass in String parameter and return int //The 100 entered in the text box on the web page is actually the string "100". The number 100 is required to be stored in the background database. At this time, the java program needs to convert the string "100" into the number 100. int anInt = Integer.parseInt("123");// String -- (conversion) - > int //int anInt = Integer.parseInt("text")// NumberFormatException System.out.println(anInt + 100);//223 //valueOf method (understand) //static Integer valueOf(int i) -- static method, passing in int parameter and returning Integer Integer integer1 = Integer.valueOf(100); //int -- (conversion) - > integer System.out.println(integer1.intValue());//100 //static Integer valueOf(String s) -- static method. Pass in String parameter and return Integer Integer integer2 = Integer.valueOf("200");//String -- (conversion) - > integer System.out.println(integer2.intValue());//200 //-------------------The following is for understanding------------------- //static String toBinaryString(int i) -- Static: convert decimal to binary string System.out.println(Integer.toBinaryString(3));//"11" //static String toOctalString(int i) -- Static: convert decimal to octal string System.out.println(Integer.toOctalString(9));//"11" //static String toHexString(int i) -- Static: convert decimal to hexadecimal string // 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13 14 15 16 17 18 19 1a 1b System.out.println(Integer.toHexString(17));//11 //public String toString() {return getClass().getName() + "@" + Integer.toHexString(hashCode());} System.out.println(new Object());//java.lang.Object@1b6d3586 } }
💨💨💨------ String int Integer conversion of three types
public class IntegerTest05 { public static void main(String[] args) { // String int Integer three types of mutual conversion test //Convert String to int Integer int i1 = Integer.parseInt("100"); Integer integer1 = Integer.valueOf("100"); //Convert int to String Integer String s1 = String.valueOf(100); String s2 = 100 + ""; Integer integer2 = new Integer(100); Integer integer3 = 100; Integer integer4 = Integer.valueOf(100); //Integer to int String int i2 = integer3.intValue(); int i3 = integer3; String s3 = String.valueOf(integer3); } }