Java wrapper classes, boxing and unboxing, conversion of primitive data types and strings

1. Packaging

1.1. Overview

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

2. Features of packaging class:

(1) All wrapper classes are final, so their subclasses cannot be created.
(2) The wrapper class is an immutable class. After the object of a wrapper class is created, the basic type data it contains cannot be changed.


     //1.基本数据类型和包装类型       
        int a1 = 5;
        Integer a2 = new Integer("123");
        System.out.println(a1);
        System.out.println(a2);

1.2 Integer class 

  • Integer class overview

    Wraps a value of primitive type int in an object

  • Integer class constructor and static method

 

//public Integer(int value):根据 int 值创建 Integer 对象(过时)
Integer i1 = new Integer(100);
System.out.println(i1);

//public Integer(String s):根据 String 值创建 Integer 对象(过时)
Integer i2 = new Integer("100");
//Integer i2 = new Integer("abc"); //NumberFormatException
System.out.println(i2);
System.out.println("--------");

//public static Integer valueOf(int i):返回表示指定的 int 值的 Integer 实例
Integer i3 = Integer.valueOf(100);
System.out.println(i3);

//public static Integer valueOf(String s):返回保存指定String值的Integer对象 
Integer i4 = Integer.valueOf("100");
System.out.println(i4);
/*
            public static string tobinarystring(int i) 得到二进制
            public static string tooctalstring(int i) 得到八进制
            public static string toHexstring(int i) 得到十六进制
            public static int parseInt(string s) 将字符串类型的整数转成int类型的整数
 */

//1.把整数转成二进制,十六进制
String str1 = Integer.toBinaryString(100);
System.out.println(str1);//1100100

//2.把整数转成八进制
String str2 = Integer.toOctalString(100);
System.out.println(str2);//144

//3.把整数转成十六进制
String str3 = Integer.toHexString(100);
System.out.println(str3);//64

//4.将字符串类型的整数转成int类型的整数
//强类型语言:每种数据在java中都有各自的数据类型
//在计算的时候,如果不是同一种数据类型,是无法直接计算的。
int i = Integer.parseInt("123");
System.out.println(i);
System.out.println(i + 1);//124
//细节1:
//在类型转换的时候,括号中的参数只能是数字不能是其他,否则代码会报错
//细节2:
//8种包装类当中,除了Character都有对应的parseXxx的方法,进行类型转换
String str = "true";
boolean b = Boolean.parseBoolean(str);
System.out.println(b);

 1.3 Packing and unpacking

The process of converting back and forth between basic types and corresponding wrapper objects is called "boxing" and "unboxing":

Boxing : Convert basic data types into corresponding packaging class objects.
Unboxing : Convert the wrapper class to the corresponding basic data type.

 

Take Integer and int as an example:

Basic data type ----> wrapper object

   //装箱
        Integer a3 = new Integer(8);//使用构造函数函数
        Integer a4 = Integer.valueOf(8);//使用包装类中的valueOf方法

 Wrapper object ----> basic data type

  //拆箱
        int a5 = a3.intValue();

1.4 Automatic boxing and automatic unboxing

Since we often need to convert between basic types and wrapper classes, starting from Java 5 (JDK 1.5), the boxing and unboxing of basic types and wrapper classes can be done automatically.  


        //自动装箱
        Integer a6 = a1;
        //自动拆箱
        int a7 = a3;

1.5 Conversion of basic data types and strings

Basic type conversion to String

 There are three ways to convert basic types to strings :

① Use the toString() method in the wrapper class

② Use the valueOf() method of the String class

③ Add empty characters

    public static void main(String[] args) {
        //int --- String
        int number = 100;
        //方式3
        String s1 = number + "";
        System.out.println(s1);
        //方式2
        //public static String valueOf(int i)
        String s2 = String.valueOf(number);
        System.out.println(s2);
        System.out.println("--------");
    }

String is converted to the corresponding basic type

① Use the parseXXX() method in the wrapper class

Except for the Character class, all other wrapper classes have a parseXxx static method that can convert a string parameter to the corresponding basic type:

 ② Use the valueOf() method in the wrapper class

int j = Integer.parseInt(i);//使用包装类中的parseXXX()方法

int j = Integer.valueOf(i);//使用包装类中的valueOf()方法

 Note: If the content of the string parameter cannot be converted to the corresponding basic type, a lawanang.NumberFormatException will be thrown.

base conversion 

 Character class

1. Overview of the Character class:

The Character class wraps a char value in an object and is used to operate on a single character.
This class provides several methods to manipulate characters, to determine the character's class (lowercase letters, numbers, etc.), to convert characters from uppercase to lowercase, etc.

2. The construction method of the Character class:

public Character(char value): Convert char type to Character class.

3. Methods of the Character class

Guess you like

Origin blog.csdn.net/weixin_67224308/article/details/128042113