Wrapping class && basic type and string conversion

Java provides two type systems, basic types and reference types. The use of basic types is efficiency. However, in many cases, objects are created and used because objects can do more functions. If we want our basic types to operate like objects, You can use the packaging class corresponding to the basic type, as follows:

Knowledge point recall: The generic requirements in ArrayList can only be reference types, not basic types. Therefore, the basic types need to be automatically boxed and converted to packaging types.

Packaging class: You can use a class to pack basic types of data and define some methods in the class. This class is called a packaging class.

You can use the methods in the class to manipulate these basic types of data.

basic type Corresponding packaging class (located in the java.lang package)
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

 

The process of conversion between basic types and corresponding packaging objects is called "boxing" and "unboxing":

Packing: Convert from the basic type to the corresponding packaging object .

Unboxing: Convert from packaging objects to corresponding basic types.

Let's take int and Integer as examples:

Packing:

Construction method: Integer(int value) constructs a newly allocated Integer object , which represents the specified int value.

                  Integer(String s) constructs a new Integer object , which represents the int value indicated by the String parameter. ( This string must be a basic type of string, otherwise an exception will be thrown )

Static method: static Integer valueOf(int i) returns an Integer instance representing the specified int value .

                  static Integer valueOf(String s) returns an Integer object that holds the value of the specified String .

Unboxing:

Member method: int intValue() returns the value of this Integer in int type.

public static void main(String[] args) { 
    //Boxing: Convert from the basic type to the corresponding packaging class object. 
    //Construction method: Integer(int value) constructs a newly allocated Integer object, which represents the specified int value. 
    Integer integer1 = new Integer(1);// The method is marked and obsolete 
    System.out.println(integer1); //1 The print object is not an address value, and the toString() method is rewritten 

    //Integer(String s) structure A new Integer object 
    Integer integer2 = new Integer("222"); 
    System.out.println(integer2);//222 

    //Static method: static Integer valueOf(int i) returns an Integer instance representing the specified int value . 
    Integer integer3 = Integer.valueOf(3); // static method, the class name directly calls 
    System.out.println(integer3);//3 

    //static Integer valueOf(String s) returns an Integer object that holds the value of the specified String . 
    Integer integer4 = Integer.valueOf("222"); // static method, directly call the class name
    System.out.println(integer4); // 222 
 
    // Unboxing
    // Member method: int intValue() returns the value of this Integer in int type. 
    int i = integer1.intValue(); 
    System.out.println(i); //1 
}

Auto-boxing and auto-unboxing

Since it is often necessary to convert between basic types and packaging classes, starting from JDK 1.5+, the boxing and unpacking actions of basic types and packaging classes can be completed automatically.

private static void demo01(){ 
    // automatic boxing int-->Integer 
    // equivalent to Integer integer = new Integer(1) 
    Integer integer = 1; 
    System.out.println(integer); 

    // automatic unboxing Integer- ->int 
    // in is a packaging class and cannot be directly involved in calculations. It can be automatically converted to basic data types and then calculated 
    // Integer + int = Integer 
    // --> int + int = Integer is automatically unboxed, which is equivalent to Integer .intValue() + int 
    //--> int = Integer 
    //-->Integer = Integer auto-boxing 
    integer = integer + 2; 
    System.out.println(integer); 
}

Conversion between basic types and strings

1. Convert the basic type to String

There are three ways to convert basic types to String:

A. The basic type can be directly connected with "", such as 34+""

B. The static method toString (parameter) of the wrapper class is not related to the overload of toString () of the Object class .

static String toString(int i) returns a String object of a specified integer.

C.String static method valueOf (parameter)

static String valueOf(int i) returns the string representation of the int parameter.

// Convert the basic type to a string 
// 1. The value of the basic type + "" 
String str1 = 23 + ""; 
System.out.println("str1:"+str1); // 23 
// 2. Packaging class The static method toString(parameter) 
String str2 = Integer.toString(3); 
System.out.println("str2:"+str2); // 3 
// 3. The static method of String class valueOf(parameter) 
String str3 = String.valueOf(23); 
System.out.println("str3:"+str3); // 23

2. Convert String type to basic type

Except for the Charater class , all other wrapper classes have parseXxx static methods that can convert string parameters to the corresponding basic types:

Such as: public static byte parseByte(String s): Convert the string parameter to the corresponding basic type.

//The string is converted to the basic type 
int num1 = Integer.valueOf("222"); 
float num2 = Float.valueOf("22.3");

Guess you like

Origin blog.csdn.net/wardo_l/article/details/113919279