Packaging, automatic packing and unpacking of packaging

target

1) The role of packaging,

2) Master the automatic packing and unpacking operation of packaging.

3) Master the conversion operation of packaging class.

 

concrete content

Everything is an object, and the eight basic data types are not objects.

Wrap Int into a class, so that a class can manipulate basic data types in the form of objects.

Packaging

The eight wrapper classes do not all inherit the same parent class.

1) Integer, byte, float, double, short and long all belong to subclasses of the Number class. The Number class itself provides a series of returns

Operations of the above six basic data types.

2) Character is a subclass of Object.

3) Boolean is a subclass of Object.

Function of Number class

Then, the Number class is an abstract class, which is mainly used to change the content in the digital packaging class into a basic data type.

The main operation methods are as follows:

Take Integer and Float as examples:

Packing and unpacking

1) Changing a basic data type into a wrapper class is called boxing.

2) Changing the type of packaging class to basic data type is called unpacking.

public class WrapperDemo01{
    public static void main(String args[]){
        int x = 30 ;        // Basic data type
        Integer i = new Integer(x) ;    // Packing: change the basic data type to packing class
        int temp = i.intValue()    ;// Unpacking: change a packing class into a basic data type
    }
};

Let's take decimals as an example:

public class WrapperDemo02{
    public static void main(String args[]){
        float f = 30.3f ;        // Basic data type
        Float x = new Float(f) ;    // Packing: change the basic data type to packing class
        float y = x.floatValue()    ;// Unpacking: change a packing class into a basic data type
    }
};

Before JDK1.5, for the program itself, the wrapper class cannot directly perform operations such as "+, -, *, /, + +, -" because it is a class,

Automatic packing and unpacking, that is, it can be automatically transformed from int -- > integer type, and automatic unpacking is automatically transformed from integer -- > int.

public class WrapperDemo03{
    public static void main(String args[]){
        Integer i = 30 ;    // Auto boxing to Integer
        Float f = 30.3f ;    // Automatic packing into Float
        int x = i ;            // Automatic unpacking is int
        float y = f ;        // Automatic unpacking is float
    }
};

There is another biggest feature in the packaging category,

Changes the string to the specified data type.

For example, a string composed entirely of numbers becomes data of type int or float.

The following two methods are provided in the Integer and Float classes:

1)Integer class (string to int type): public   static   int   parseInt(String s);

2)Float class (string to float type): public   static   float   parseFloat(String s);

The method modified by static keyword is called directly by the class

Note: the String s here must be composed of numbers.

public class WrapperDemo04{
    public static void main(String args[]){
        String str1 = "30" ;    // A string of numbers
        String str2 = "30.3" ;    // A string of numbers
        int x = Integer.parseInt(str1) ;    // Change string to int
        float f = Float.parseFloat(str2) ;    // Change string to int
        System.out.println("Integer power:" + x + " * " + x + " = " + (x * x)) ;
        System.out.println("Decimal power:" + f + " * " + f + " = " + (f * f)) ;
    }
};
Operation results:
Integer power: 30 * 30 = 900
 Decimal power: 30.3 * 30.3 = 918.08997

Get string from initialization parameter:

public class WrapperDemo05{
    public static void main(String args[]){
        int x = Integer.parseInt(args[0]) ;    // Change string to int
        float f = Float.parseFloat(args[1]) ;    // Change string to int
        System.out.println("Integer power:" + x + " * " + x + " = " + (x * x)) ;
        System.out.println("Decimal power:" + f + " * " + f + " = " + (f * f)) ;
    }
};

summary

1) There are 8 wrapper classes of basic data types in java, which can operate basic data types in the form of classes.

2) The process of changing a basic data type into a package type is called boxing, and the process of changing a package type into a basic data type is called unpacking.

3) After JDK1.5, automatic packing and unpacking functions are provided.

4) Using wrapper classes, you can convert strings to basic data types.

Tags: Java

Posted on Fri, 17 Sep 2021 17:22:31 -0400 by davelr459