Day32: Detailed packaging class

Packaging

1.1 Overview of packaging classes

The reference data type corresponding to the value of the basic data type is called a wrapper class.

Since our basic data types are not used as objects, but objects are often used as parameters, Java provides wrapper classes to wrap basic data types into an object.

Each basic data type has a corresponding wrapper class, and the default value is null

byte - Byte The Byte class wraps the value of the primitive type byte in an object

short-Short

int-Integer

long-Long

float-Float

double-Double

char-Charactar

boolean-Boolean

Except for int and char, the wrapper classes of other basic types have the same names as the corresponding basic data types, and the first letters are all capitalized.

1.2 Boxing and unboxing of type conversion

Let's first perform memory analysis on boxing and unboxing:

1.2.1 Boxing for type conversion

Boxing is converting a primitive type to a reference type.

Let's take the int type as an example as a demonstration

The wrapper class of the int type is the Integer class; how to convert the int type to the Integer type?

  • Method 1: Through the constructor of the Integer class: Integer(int value)

  • Method 2: Use the valueOf method (static method) of the Integer class to use the value of the int class as a parameter: valueOf(int i)

public class Test{
    public static void main(String[] args){
    int a=10;
    Integer i1=new Integer(a);//Boxing is achieved by passing parameters through the constructor of Integer
    Integer i2=Integer.valueOf(a);//Call the Integer.valueOf method to return an Integer instance of the int value specified by Integer
    Integer i3=Integer.valueOf(10);
    }
}

1.2.2 Unboxing of type conversion

Unboxing is about converting reference types to primitive types

Let's take the int type as an example as a demonstration

  • Through the intValue method of the Integer class (non-static method, used for instances): intValue(): use the value of Integer as an int
public class Test{
    public static void main(String[] args){
    int a=10;
    Integer i2=Integer.valueOf(a);//Call the Integer.valueOf method to implement boxing of basic types
    int a1=i2.intValue();//Call the intValue method through the Integer instance object for unboxing
    }
}

1.2.3 Automatic boxing and unboxing

After JDK1.5, it supports automatic boxing and unboxing

public class Test{
    public static void main(String[] args){
    int a=10;
    Integer i2=a;//autoboxing
    int b=i2;//Automatic unboxing
    }
}

1.3 Mutual conversion between types

Basic types and strings can be converted to each other;

Let's take the conversion between int type and String type as an example

public class Test{
    public static void main(String[] args){
        //Convert int type to String type
        int a=10;
        //The first way: through the + sign
        String st1=a+" ";
        System.out.println(st1);//10
        //The second way: (static method) toString(); Transformation is achieved by calling the int wrapper class
        String st2=Integer.toString(a);
        System.out.println(st2);//10
        //expand
        //The toString(); overloaded method in the int wrapper class can calculate the values ​​​​under different bases of the integer type
        //Calculate the value of 16 before hexadecimal
        System.out.println(Integer.toString(16));//10
        
        
        
        //String to int type   
         String st3="19971007";
        //At this time, it is in the form of a string; Note that if the string is to be converted to an int type, then its string content must only be int type, otherwise a NumberFormatException will be thrown
        //Transformation method: Integer.paseInt(); Static method What type of transfer is used and what type of wrapper class is used to call the method
         int a3=Integer.parseInt(st3);
         System.out.println(a3);//19971007
        
        //expand
        /*In the conversion of strings to basic types, the boolean type is quite special, because the values ​​​​of the boolean type are only true and false,
        When we transform the string, it will be converted to "true" only when the content of the string is "true", and when the content of the string is false, or it is not true, it will be false when it is converted back
        */
        
        String st4="true";
        String st5-="Construction guy"
        boolean b=Boolean.paseBoolean(st4);
        System.out.println(b);//true.
        System.out.println(Boolean.paseBooleam(st5));//false
        
    }
}

1.4 Integer buffer

The instance created by the valueOf method in the integer wrapper class can be enjoyed, and the integer interval value is -128-127; once it exceeds the range, it will no longer be shared.

public class Test{
    public static void main(String[] args){
        //Wrapper class objects instantiated through the constructor are independent and will not be shared
        Integer i1=new Integer(10);
        Integer i2=new Integer(10);
        System.out.println(i1==i2);//false
        //"==" compares values ​​for basic types, and compares heap memory addresses for reference types; both i1 and i2 are new, and the memory addresses must be different, so they will not be equal!
        
        
        //Objects passed through the valueOf instance will be shared in the integer buffer
        Integer a=Integer.valueOf(88);
        Integer b=Integer.valueOf(88);
        Integer c=Integer.valueOf(200);
        Integer d=Integer.valueOf(200);
        System.out.println(a==b);//true 88 In valueOf's integer buffer, objects are shared
        System.out.println(c==d);//If false 200 is not in the integer buffer of valueOf, a new heap memory will be created, and the memory addresses will be inconsistent, and naturally they will not be equal!
    }
}

Posted by 5kyy8lu3 on Mon, 05 Dec 2022 18:47:23 +0300