Study notes: packaging

Keywords: Java Back-end

Learning content: packaging class (the examples are int type, and other types are painted according to gourd)


Two new features have been added in Java 5, that is, automatic boxing and unpacking. Because basic types are widely used, but Java is an object-oriented language, it provides support for packaging types.

There are eight wrapper classes: byte (byte), short (short), Integer (int), long (long), float (float), double (double), Character (char) and boolean (boolean) (their corresponding basic data types are in parentheses).

So why provide 8 more packaging categories?

JAVA is an object-oriented language. The parameters in many classes and methods need to use objects (such as collections), but the basic data types are not object-oriented, which causes a lot of inconvenience. For example: List < int > = new ArrayList < > ();, Can't compile. In order to solve this problem, we have introduced a wrapper class. As the name suggests, it is to "wrap" the basic type to make it have the nature of an object, including adding attributes and methods, which are located under the java.lang package.

boxing and unboxing

Packing: the process of converting the basic data type to the reference data type of the corresponding packing class is called "packing".

Integer i =new Integer(123);

Unpacking: the process of converting the referenced data type of the packaging class to the corresponding basic data type is called "unpacking"

float f = i.floatValue();

Methods commonly used in packaging

Among the eight wrapper classes: Byte, Short, Integer, Long, Float and Double, the parent class is Number. The parent class of Character and Boolean is Object.

Number is an abstract class and cannot instantiate an object. The methods are:
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 float.
abstract int intValue() returns the specified value as int.
abstract long longValue() returns the specified value as a long.
short shortValue() returns the specified value as a short.

Constructor in Integer wrapper class: Integer(int value) and Integer(String s)
(Note: the parameters passed in Integer(String s) can only be numeric objects of String type)

public class Test01 {
    public static void main(String[] args) {
        Integer i =new Integer(123);
        System.out.println(i);//Output result 123
        Integer j =new Integer("123");
        System.out.println(j);//Output result 123
        //Integer k =new Integer("China"); if an error is reported, "China" is not a number and cannot be packaged as an integer
        
        Double x=new Double(123);
        System.out.println(x);//Output 123.0
        Double y =new Double("123");
        System.out.println(y);//Output 123.0
    }
}

Constants in Integer wrapper class: MAX_VALUE, MIN_VALUE.

public class Test01 {
    public static void main(String[] args) {
        System.out.println("int Maximum value of"+Integer.MAX_VALUE);//Output result: maximum value of int 2147483647
        System.out.println("int Minimum value of"+Integer.MIN_VALUE);//Output result: minimum value of int - 2147483648
        System.out.println("byte Maximum value of"+Byte.MAX_VALUE);//Output result: the maximum value of byte is 127
        System.out.println("byte Minimum value of"+Byte.MIN_VALUE);//Output result: minimum value of byte - 128
    }
}

Methods commonly used in Integer wrapper classes:

  1. static int parseInt(String s) converts a string to type int
public class Test01 {
    public static void main(String[] args) {
        int x =Integer.parseInt("123");
        System.out.println(x);//Output result 123

        double y=Double.parseDouble("123");
        System.out.println(y);//Output 123.0

    }
}
  1. static String toBinaryString(int i) converts decimal to binary string.
  2. static String toHexString(int i) converts decimal to hexadecimal string.
  3. static String toOctalString(int i) converts decimal to octal string.
public class Test01 {
    public static void main(String[] args) {
        //Convert decimal to binary string
        String x = Integer.toBinaryString(3);
        System.out.println(x);//Output result: 11

        //Convert decimal to hexadecimal string
        String y =Integer.toHexString(17);
        System.out.println(y);//Output result: 11

        //Convert decimal to octal string
        String z=Integer.toOctalString(10);
        System.out.println(z);//Output result: 12
    }
}
  1. static Integer valueOf(int i) converts the int type to Integer
  2. static Integer valueOf(String s) converts String type to Integer
public class Test01 {
    public static void main(String[] args) {
    	//int--->Integer 
       Integer i1=Integer.valueOf(100);
		System.out.println(i1);//Output result: 100
		//String--->Integer 
       Integer i2=Integer.valueOf("100");
       System.out.println(i2);//Output result: 100
    }
}

Automatic packing and unpacking

After jdk1.5, automatic packing and automatic unpacking are supported.
Automatic packing: the basic data type is automatically converted to the packing type.
Automatic unpacking: the package type is automatically converted to the basic data type.

public class Test01 {
    public static void main(String[] args) {
    	//Automatic packing
    	//Here, a basic data type is not directly assigned to a reference data type,
		//Code principle or Integer i =new Integer(100);
		//It's just that we don't need to enter the new Integer object manually.
        Integer i=100;
        
        //Automatic unpacking
        //The principle is the same as automatic packing
        int j=i;
    }
}

reflection:

public class Test01 {
    public static void main(String[] args) {
        Integer a=1000;//Actually Integer a =new Integer(1000);
        Integer b=1000;//Actually Integer b =new Integer(1000);
        System.out.println(a==b);//The output result is false
    }
}

The reason why the output result is false is that "= =" compares the memory addresses of the two references, while a and b are actually references to an Integer object, so the memory addresses of a and b are compared. Because the automatic boxing is actually a new object, the memory addresses of a and b are different, and false is output.

public class Test01 {
    public static void main(String[] args) {
        Integer x=127;
        Integer y=127;
        System.out.println(x==y);//The output result is true
    }
}

According to the above explanation, false should be output here. Why is true output?

Explanation: in java, in order to improve the execution efficiency of the program, the wrapper objects between [- 128127] and [- 128127] are created in advance and placed in the "integer constant pool" in the corresponding method area. The purpose is to take the data in this area directly from the integer constant pool instead of new.

The object memory address saved in the x variable is the same as the object memory address saved in the y variable.

Mutual conversion of String, int and Integer types


Example:

public class Test01 {
    public static void main(String[] args) {
        //String--->int
       int i1=Integer.parseInt("100");//i1 is a number of 100
        System.out.println(i1+1);//Output result: 101

        //int--->String
        String s2=i1+"";//"100" string
        System.out.println(s2+1);//"1001"

        //int--->Integer
        Integer x=1000;//Automatic packing

        //Integer--->int
        int y=x;//Automatic unpacking

        //String--->Integer
        Integer k=Integer.valueOf("123");

        //Integer--->String
       String e=String.valueOf(k);
    }
}

Posted by skyriders on Thu, 25 Nov 2021 10:15:13 -0800