A concept,

To put it simply:

  • Boxing automatically converts the base data type to wrapper type;

  • Unpacking automatically converts wrapper types to primitive data types.

Such as:

Integer i = 10; Int n = I; / / split open a caseCopy the code

Two, the introduction of basic packaging classes

2.1 Generation of basic type packaging classes

In the actual use of the program, the data input by the user on the program interface is stored as a string type. In the program development, we need to convert string data into specified basic data types according to requirements, such as age needs to be converted into int type, test scores need to be converted into double type and so on

Overview: The advantage of encapsulating a basic data type into an object is that you can define more functional methods in the object to manipulate that data

One of the common operations: conversion between primitive data types and strings

2.2 Common methods of wrapping classes

2.2.1 Other methods of the Integer class
  • Decimal to binary toBinarString(int)

  • Decimal to octal toOctalString(int)

  • Decimal to hexadecimal toHexString(int)

[Note] : All three methods return a String

public static void function_1(){
	System.out.println(Integer.toBinaryString(99));
    System.out.println(Integer.toOctalString(99));
	System.out.println(Integer.toHexString(999));
}
Copy the code
2.2.2 Constructor of the Integer class
  • public Integer( int value)

  • Public Integer (String s): Note: This String consists of numbers

Public class Demo2 {public static void main(String[] args) {// Type 1: int I =10; Integer ii =new Integer(10); System.out.println(ii); // Mode 2: String s= "111"; // Integer iii =new Integer(s); System.out.println(iii); }}Copy the code
2.2.3 Conversion of int and String
public class Democrat { public static void main(String[] args) { //int -->String int number =100; String s1=""+number; System.out.println(s1); S2 = string.valueof (number); s2 = string.valueof (number) System.out.println("s2 = " + s2); Integer I = new Integer(number); System.out.println(i.toString()); // 4: String s4= integer.toString (number); System.out.println(s4); System.out.println("-------------------------------"); //String -->int String s="100"; // Type 1: //String--Integer-- int Integer ii =new Integer(s); Int x=ii.intValue(); // Convert an Integer to an int system.out.println (x); Float public static int parseInt(String s) int y= integer.parseint (s); float public static int parseInt(String s) int y= integer.parseint (s); float public static int parseInt(String s) System.out.println(y); }}Copy the code

3. Principle of packing and unpacking

The packing process is implemented by calling the valueOf method of the wrapper, and the unpacking process is implemented by calling the xxxValue method of the wrapper.

Automatic boxing, unboxing benefits: Base types and reference classes are directly computed

[Note] : It is recommended to check whether the value is null before using it

Automatic packing:

  • Using integer.valueof (Integer value) returns an Integer object that encapsulates the Integer value
  • That is, to convert a base type to a reference type

Automatic unpacking:

  • Use the integer.intValue () object to return the Integer value encapsulated in the Integer object
  • Convert a reference type to a primitive data type

Four, a few topics

1, will be automatically unpacked before comparison

class AutoUnboxingTest { public static void main(String[] args) { Integer a = new Integer(3); Integer b = 3; // automatically box 3 into Integer type int c = 3; System.out.println(a == b); // false Two references do not refer to the same object system.out.println (a == c); // int int int int int int int int int int intCopy the code

2. What is the output?

public class Test03 { public static void main(String[] args) { Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150; System.out.println(f1 == f2); System.out.println(f3 == f4); }}Copy the code

All four variables are Integer object references, so the == operation below compares references rather than values. What is the nature of packing? When we assign an int value to an Integer object, valueOf, the static method of the Integer class, is called. Integer literals with values between -128 and 127 will not be new Integer objects, but will refer directly to Integer objects in the constant pool

F1 = = true f2; F3 == the value of f4 is false.