Dry goods|Java packaging concepts and usage

In order to make the basic data types have the characteristics of objects, Java provides a wrapper class for each basic data type, so that we can manipulate the basic data types like operating objects.


Packaging concept

The basic data types are very convenient to use, but there is no corresponding method to manipulate the data of these basic data types. You can use a class to pack the basic data types and define a method in the class . This class is called a wrapper class. We can use the class To manipulate these basic types of data.

Insert picture description here

1. Packing

Packing : Pack basic types of data into packaging classes (basic types of data -> packaging classes)
Insert picture description here

Construction method:
Integer(int value) Constructs a newly allocated Integer object, which represents the specified int value.
Integer(String s) Constructs a newly allocated Integer object, which represents the int value indicated by the String parameter.
The passed string must be a basic type of string, otherwise an exception "100" will be thrown. Correct "a".
Static method:

        static Integer valueOf(int i) 返回一个表示指定的 int 值的 Integer 实例。
        static Integer valueOf(String s) 返回保存指定的 String 的值的 Integer 对象。

Code example:

public class 包装类的概念 {
    
    
    public static void main(String[] args) {
    
    
        //装箱:把基本类型的数据,包装到包装类中(基本类型的数据->包装类)
        //构造方法
        Integer in1 = new Integer(1);//有划线表示方法以及过时
        System.out.println(in1);
      

        Integer in2 = new Integer("1");
        System.out.println(in2);
       //静态方法
        Integer in3 = Integer.valueOf(1);
        System.out.println(in3);


        Integer in4 = Integer.valueOf("1");
        System.out.println(in4);

Two, unboxing

Unboxing: Take out the data of the basic type from the package class (package class -> data of the basic type).
Member method:
int intValue() returns the value of the Integer as an int.

 //拆箱:在包装类中取出基本类型的数据(包装类->基本类型的数据)
        int i = in1.intValue();
        System.out.println(i);

Three, automatic unpacking and automatic packing

Automatic boxing and automatic unboxing:
basic types of data and packaging classes can be automatically converted between
new features after JDK1.5

Auto-boxing : directly assign an integer of type int to a packaging class
code example

public class Hello Worid {
    
    
    public static void main(String[] args) {
    
    
        /*
            自动装箱:直接把int类型的整数赋值包装类
            Integer in = 1; 就相当于 Integer in = new Integer(1);
         */
        Integer in = 1;

        /*
            自动拆箱:in是包装类,无法直接参与运算,可以自动转换为基本数据类型,在进行计算
            in+2;就相当于 in.intVale() + 2 = 3
            in = in.intVale() + 2 = 3 又是一个自动装箱
         */
        in = in+2;

        ArrayList<Integer> list = new ArrayList<>();
        /*
            ArrayList集合无法直接存储整数,可以存储Integer包装类
         */
        list.add(1); //-->自动装箱 list.add(new Integer(1));

        int a = list.get(0); //-->自动拆箱  list.get(0).intValue();
    }
}

Fourth, the mutual conversion between basic types and string types

Mutual conversion between basic types and string types
Basic types -> String (String)
1. Basic type value + "" The simplest method (commonly used in work)

2. The static method toString (parameter) of the wrapper class, not the toString() overload of the Object class

static String toString(int i) Returns a String object representing the specified integer.

3. The static method valueOf of the String class (parameter)
static String valueOf(int i) returns the string representation of the int parameter.

String (String)->Basic Type
Use the static method parseXXX("string") of the wrapper class;

Integer类: static int parseInt(String s)

Double类: static double parseDouble(String s)

public class 基本类型与字符串类型之间的相互转换 {
    
    
    public static void main(String[] args) {
    
    


        //基本类型->字符串(String)
        //方法一
        int i1 = 100;
        String s1 = i1+"";
        System.out.println(s1+200);//打印输出为 100200
        //方法二
        String s2 = Integer.toString(100);
        System.out.println(s2+100);//打印输出为 100200
        //方法三
        String s3 = String.valueOf(100);
        System.out.println(s3+200);//打印输出为 100200



        // 字符串(String)->基本类型
        // Integer类: static int parseInt(String s)
        int i = Integer.parseInt(s1);
        System.out.println(i-10);//打印输出为 100200
        int a = Integer.parseInt("a");// NumberFormatException  异常 英文字符转换为整型
        System.out.println(a);
        

    }
}

Guess you like

Origin blog.csdn.net/weixin_46235428/article/details/109260282