Java Self-learning - Number and String Boxing and Unpacking

Keywords: Java

Basic types of boxing and unboxing in Java

Step 1: Encapsulation classes

All basic types have corresponding class types
For example, the class corresponding to int is Integer.
This class is called encapsulation class.

package digit;
 
public class TestNumber {
 
    public static void main(String[] args) {
        int i = 5;
         
        //Convert a basic type of variable to an Integer object
        Integer it = new Integer(i);
        //Convert an Integer object to a basic type of int
        int i2 = it.intValue();
         
    }
}

Step 2: Number class

Digital encapsulation classes include
Byte,Short,Integer,Long,Float,Double
These classes are subclasses of the abstract class Number

package digit;
 
public class TestNumber {
 
    public static void main(String[] args) {
        int i = 5;
         
        Integer it = new Integer(i);
        //Integer is a subclass of Number, so print true
        System.out.println(it instanceof Number);
    }
}

Step 3: Basic type encapsulation classes

package digit;
 
public class TestNumber {
 
    public static void main(String[] args) {
        int i = 5;
 
        //Basic type to encapsulation type
        Integer it = new Integer(i);
         
    }
}

Step 4: Packaging classes to basic types

package digit;
 
public class TestNumber {
 
    public static void main(String[] args) {
        int i = 5;
 
        //Basic type to encapsulation type
        Integer it = new Integer(i);
         
        //Conversion of encapsulation type to basic type
        int i2 = it.intValue();
         
    }
}

Step 5: Automatic packing

Instead of calling the constructor, the basic type is automatically converted to the class type by the = symbol, which is called boxing.

package digit;
 
public class TestNumber {
 
    public static void main(String[] args) {
        int i = 5;
 
        //Basic type to encapsulation type
        Integer it = new Integer(i);
         
        //Automatic conversion is called packing
        Integer it2 = i;
         
    }
}

Step 6: Automatically unpacking

Without calling Integer's intValue method, it automatically converts to int type by = and is called unboxing.

package digit;
  
public class TestNumber {
  
    public static void main(String[] args) {
        int i = 5;
  
        Integer it = new Integer(i);
          
        //Conversion of encapsulation type to basic type
        int i2 = it.intValue();
         
        //Automatic conversion is called unpacking
        int i3 = it;
          
    }
}

Step 7: Maximum and minimum of int

The maximum value of int can be obtained by its corresponding encapsulation class Integer.MAX_VALUE

package digit;
  
public class TestNumber {
  
    public static void main(String[] args) {
 
        //Maximum of int
        System.out.println(Integer.MAX_VALUE);
        //Minimum value of int      
        System.out.println(Integer.MIN_VALUE);
          
    }
}

Practice: Boxing and unboxing

  1. Auto-unloading and auto-loading of byte, short, float and double

  2. Can automatic unloading and packing be carried out between byte and Integer?

  3. Getting the maximum value of byte through Byte

Answer:

package digit;
 
public class TestNumber {
 
    public static void main(String[] args) {
        // 1. Auto-unloading and auto-loading of byte, short, float and double
        byte b = 1;
        short s = 2;
        float f = 3.14f;
        double d = 6.18;
 
        // Automatic boxing
        Byte b1 = b;
        Short s1 = s;
        Float f1 = f;
        Double d1 = d;
        // Automatic dismantling
        b = b1;
        s = s1;
        f = f1;
        d = d1;
 
        // 2. Can automatic unloading and packing be carried out between byte and Integer?
        Integer i1 = b; //You can't automatically pack byte directly into Integer
        b = new Integer(1); //Nor can Integer be automatically disassembled into byte s
         
        // 3. Get the maximum byte by Byte
        System.out.println(Byte.MAX_VALUE);
         
    }
}

Posted by SurgeProto on Wed, 02 Oct 2019 13:32:55 -0700