Java learning road-packaging class

Java learning road-packaging class

Introduction

The data types int and double in Java are not objects, and the methods provided by Object cannot be obtained through upcasting. Due to such characteristics, basic data types cannot participate in transformation, generic, reflection and other processes. To make up for this shortcoming, Java provides packaging classes.

The 8 basic data types in Java have corresponding packaging classes:

Basic data type Packaging
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

1. The difference between basic data types and packaging

  1. The definition is different: the packaging class belongs to the object, the basic data type is not;
  2. The declaration and usage methods are different: the wrapper class uses new initialization, and the definition of some collection classes cannot use basic data types, such as ArrayList;
  3. The initial value is different. The default value of the packaging class is null, and the basic data type is different (see the above table for details);
  4. Different storage methods and locations result in different performance. The basic data types are stored in the stack, and the packaging classes are divided into references and instances, the references are in the stack, and the specific instances are in the heap.

Two, unboxing/packing

Unpacking/packing is actually the mutual conversion of basic types and packaging types.

  • The conversion from the basic type to the corresponding packaging class is called boxing. For example, packaging int into an object of Integer class:

    Integer i = Integer.valueOf(1); //手动装箱
    Integer j = 1; //自动装箱
    
  • The conversion of the packaging class to the corresponding basic type is called unboxing. For example, the object of the Integer class is re-simplified to int:

    Integer i0 = new Integer(1);
    int i1 = i0; //自动拆箱
    int i2 = i0.intValue(); //手动拆箱
    

Three, basic data types, packaging classes and String conversion

  • Basic data types and packaging classes to string:

    // 1. 连接运算
    String str1 = 1 + "";
    String str2 = new Integer(1) + "";
    // 2. 调用 String.valueOf()方法
    String str3 = String.valueOf(1);
    String str4 = String.valueOf(new Integer(1));
    
  • String to basic data types and packaging classes:

    // 采用包装类的 parseXxx() 方法,返回值为基本类型
    int num1 = Integer.parseInt("123");
    // 如果字符串形式不对,运行时会发生 NumberFormatException 错误
    // 如:int num1 = Integer.parseInt("12a3");
    

Fourth, memory reuse with automatic boxing

When auto-boxing, for Integer var =? , If the object pointed var assignment in the range -128 to 127, generated by the Integer object instance IntegerCache.cache()generating method, it will reuse existing objects. The shared pool operation with String is the same. The cache() method will pool the Integer objects generated in the range of -128~127. The next time it is used, it will be taken from the pool and will not be created.

Integer a1 = 1;
Integer a2 = 1;
System.out.println(a1 == a2); // true

Integer b1 = 222;
Integer b2 = 222;
System.out.println(b1 == b2); // false

Therefore, the stack pointer (attribute name) of the Integer object in this numerical range can be directly judged with ==, because the value is the same, it points to the same area. However, all data outside this interval will be instantiated on the heap by automatic boxing, and existing objects will no longer be reused. It is recommended to use the equals method for Integer judgment.

Guess you like

Origin blog.csdn.net/qq_43580193/article/details/112567404