Java's eight major packaging classes (super detailed!)

1. Packaging type names corresponding to 8 basic data types

Basic data types type of packaging
byte java.lang.Byte ( parent class Number)
short java.lang.Short ( parent class Number)
int java.lang.Integer ( parent class Number)
long java.lang.Long ( parent class Number)
float java.lang. Float (parent class Number)
double java.lang.Double ( parent class Number)
boolean java.lang.Boolean ( parent class Object)
char java.lang.Character ( parent class Object)

Note: 8 packaging categories belong to引用数据类型

2. Number method of parent class

method name effect
byte byteValue() Returns the specified value in byte form
abstract double doubleValue() Returns the specified value in double form
abstract float floatValue() Returns the specified value in float form
abstract int intValue() Returns the specified value in int form
abstract long longValue () Returns the specified value in long form
short shortValue() Returns the specified value in short form

Note: These methods are available to all subclasses of the number wrapper class and are responsible for these methods 拆箱.

3. Manual packing/manual unboxing

public class IntegerTest02 {
    
    
    public static void main(String[] args) {
    
    

        // 123这个基本数据类型,进行构造方法的包装达到了:基本数据类型向引用数据类型的转换。
        // 基本数据类型 -(转换为)->引用数据类型(装箱)
        Integer i = new Integer(123);

        // 将引用数据类型--(转换为)-> 基本数据类型
        float f = i.floatValue();
        System.out.println(f); //123.0

        // 将引用数据类型--(转换为)-> 基本数据类型(拆箱)
        int retValue = i.intValue();
        System.out.println(retValue); //123
    }
}

4. Construction method (outdated after Java 9)

Integer constructor name
Integer(int value)
Integer(String s)

Notice:

  • The construction methods of Byte, Short, Long, Float, Double, and Boolean are the same as those of Integer.
  • Character has only one constructor
Character construction method name
Character(char value)

Note: There is one more Float than Integer.

Float constructor name
Float(double value)

eg.

public class IntegerTest03 {
    
    
    public static void main(String[] args) {
    
    

        // Java9之后不建议使用这个构造方法了。出现横线表示已过时。
        // 将数字100转换成Integer包装类型(int --> Integer)
        Integer x = new Integer(100);
        System.out.println(x);

        // 将String类型的数字,转换成Integer包装类型。(String --> Integer)
        Integer y = new Integer("123");
        System.out.println(y);

        // double -->Double
        Double d = new Double(1.23);
        System.out.println(d);

        // String --> Double
        Double e = new Double("3.14");
        System.out.println(e);
    }
}

5. Packaging constants

constant properties
MAX_VALUE
MIN_VALUE

Note: Byte, Short, Integer, Long, Float, and Double are available.

eg.

public class IntegerTest04 {
    
    
    public static void main(String[] args) {
    
    
        // 通过访问包装类的常量,来获取最大值和最小值
        System.out.println("int的最大值:" + Integer.MAX_VALUE);
        System.out.println("int的最小值:" + Integer.MIN_VALUE);
        System.out.println("byte的最大值:" + Byte.MAX_VALUE);
        System.out.println("byte的最小值:" + Byte.MIN_VALUE);
    }
}

6. Automatic boxing/automatic unboxing (new feature of java5)

1. What are autoboxing and autounboxing?

  • Autoboxing: Basic data types are automatically converted into packaging classes .
  • Automatic unboxing: packaging classes are automatically converted into basic data types

2. What are the benefits of automatic boxing and automatic unboxing?

  • Easy to program
  • With automatic unboxing, the methods in the Number class are no longer needed.

eg.

public class IntegerTest05 {
    
    
    public static void main(String[] args) {
    
    
        // 900是基本数据类型
        // x是包装类型
        // 基本数据类型 --(自动转换)--> 包装类型:自动装箱
        Integer x = 900; // 等同于:Integer z = new Integer(900);
        System.out.println(x);

        // x是包装类型
        // y是基本数据类型
        // 包装类型 --(自动转换)--> 基本数据类型:自动拆箱
        int y = x;
        System.out.println(y);
    }
}

Note: The bottom layer of autoboxing is actually a new object .

public class IntegerTest05 {
    
    
    public static void main(String[] args) {
    
    
        // z是一个引用,z是一个变量,z还是保存了一个对象的内存地址。
        Integer z = 1000; // 等同于:Integer z = new Integer(1000);
        
        // 分析为什么这个没有报错呢?
        // +两边要求是基本数据类型的数字,z是包装类,不属于基本数据类型,这里会进行自动拆箱。将z转换成基本数据类型
        // 在java5之前你这样写肯定编译器报错。
        System.out.println(z + 1);//1001

        Integer a = 1000; // Integer a = new Integer(1000); a是个引用,保存内存地址指向对象。
        Integer b = 1000; // Integer b = new Integer(1000); b是个引用,保存内存地址指向对象。
        System.out.println(a == b); //false
    }
}

Notice:

  • == This operator does not trigger the automatic unboxing mechanism
  • == always determines whether the memory addresses of the two objects are the same.
  • The automatic unboxing mechanism will only + - * /be triggered when waiting for calculation.

7. (Method area) Integer constant pool

public class IntegerTest06 {
    
    
    public static void main(String[] args) {
    
    
        Integer a = 128;
        Integer b = 128;
        System.out.println(a == b); //false

        //原理:x变量中保存的对象的内存地址和y变量中保存的对象的内存地址是一样的。
        Integer x = 127;
        Integer y = 127;
        // == 永远判断的都是两个对象的内存地址是否相同。
        System.out.println(x == y); //true
    }
}

In order to improve the execution efficiency of the program in java, [-128 ~ 127]all the packaging objects between are created in advance and placed in " " in a method area 整数型常量池.

The purpose is: as long as the data in this range is used, there is no need to create new data, and it can be taken directly from the integer constant pool.

Insert image description here

8. Integer method

method name effect
static Integer decode(String nm) Convert String to Integer
static int compare(int x, int y) Compares whether two numbers are equal; if equal, returns 0; if the former is larger and the latter is smaller, 1 is returned; if the latter is larger than the previous is smaller, returns -1
static int signum(int i) Sign function; negative numbers return -1; positive numbers return 1; 0 returns 0
static String toBinaryString(int i) Convert i to binary
static String toHexString(int i) Convert i to hexadecimal
static String toOctalString(int i ) Convert i to octal
Common methods
static int parseInt(String s) Convert string to int
static Integer valueOf(String s) Convert string to Integer
String toString() Integer to String
boolean equals(Object obj) Determine whether two Integer are equal

Note: The methods for Byte, Short, Long, Float, Double and Boolean are almost the same.

eg.

class IntegerTest{
    
    
    public static void main(String[] args) {
    
    
        Integer d = Integer.decode("123");
        System.out.println(d);//自动拆箱 123

        Integer a = 100;
        Integer b = 100;

        int res1 = Integer.compare(a, b);
        System.out.println(res1);//0
        res1 = Integer.compare(-a, b);
        System.out.println(res1);//-1
        res1 = Integer.compare(a, -b);
        System.out.println(res1);//1

        System.out.println(a.equals(b));//true

        int i = Integer.parseInt("123");
        System.out.println(i);//123

        System.out.println(Integer.signum(-123));//-1
        System.out.println(Integer.signum(123));//1
        System.out.println(Integer.signum(0));//0

        System.out.println(Integer.toBinaryString(10));//1010
        System.out.println(Integer.toOctalString(10));//12
        System.out.println(Integer.toHexString(10));//a

        String s = Integer.toString(123);
        System.out.println(s);//123

        Integer int1 = Integer.valueOf("123");
        System.out.println(int1);//123
    }
}

9. Character method

method name effect
char charValue () Convert Character to char
int compareTo(Character anotherCharacter) Determine whether two Characters are equal; if equal, return 0; if the former is larger and the latter is smaller, return 1; if the latter is larger than the previous is smaller, return -1
Common methods
boolean equals(Object obj) Determine whether two Characters are equal
String toString() Convert Character to String
static boolean isDigit(char ch) Determine whether ch is a number
static boolean isLetter(char ch) Determine whether ch is a letter
static boolean isLetterOrDigit(char ch) Determine whether ch is a letter or number
static boolean isLowerCase(char ch) Determine whether ch is a lowercase letter
static boolean isUpperCase(char ch) Determine whether ch is a capital letter
static boolean isSpaceChar(char ch) Determine whether ch is a space
static Character valueOf(char c) Convert char to Character

eg.

class CharacterTest{
    
    
    public static void main(String[] args) {
    
    
        Character c = 'a';

        char res1 = c.charValue();
        System.out.println(res1);//a

        Character a = 'a';
        Character b = 'b';
        System.out.println(a.compareTo(b));//-1

        System.out.println(a.equals(b));//false

        System.out.println(Character.isDigit('1'));//true

        System.out.println(Character.isLetter('a'));//true

        System.out.println(Character.isLetterOrDigit('1'));//true
        System.out.println(Character.isLetterOrDigit('a'));//true

        System.out.println(Character.isLowerCase('a'));//true

        System.out.println(Character.isUpperCase('A'));//true

        System.out.println(Character.isSpaceChar(' '));//true

        System.out.println(c.toString());//"a"

        System.out.println(Character.valueOf('c'));//c

    }
}

Guess you like

Origin blog.csdn.net/qq_44715943/article/details/116331282