编程知识 cdmana.com

Java wrapper type

{tabs-pane label=" Automatic boxing (Auto Boxing) Automatic dismantling (Auto Unboxing)"}

Put... Directly int Turn into Integer Assignment writing of , It is called automatic packing (Auto Boxing), In turn, , hold Integer Turn into int Assignment writing of , It's called automatic unpacking (Auto Unboxing) Be careful : Autoboxing and unboxing happen only during the compile phase , The purpose is to write less code .

To put int The base type becomes a reference type , We can define one Integer class , It contains only one instance field int, such ,Integer Class can be regarded as int The wrapper class

    public class Integer {
    private int value;

    public Integer(int value) {
        this.value = value;
    }

    public int intValue() {
        return this.value;
    }
}
Integer n = null;
Integer n2 = new Integer(99);
int n3 = n2.intValue();

Packaging types are very useful ,Java The core library provides a corresponding wrapper type for each basic type

Basic types The corresponding reference type boolean java.lang.Boolean byte java.lang.Byte short java.lang.Short int java.lang.Integer long java.lang.Long float java.lang.Float double java.lang.Double char java.lang.Character

For direct use, there is no need to define

public class Main {
    public static void main(String[] args) {
        int i = 100;
        //  adopt new Operator creation Integer example ( It is not recommended to use , There will be compilation warnings ):
        Integer n1 = new Integer(i);
        //  By static method valueOf(int) establish Integer example :
        Integer n2 = Integer.valueOf(i);
        //  By static method valueOf(String) establish Integer example :
        Integer n3 = Integer.valueOf("100");
        System.out.println(n3.intValue());
    }
}

{/tabs-pane} {tabs-pane label=" Invariant class "} All wrapper types are invariant classes . We see the Integer The source code of , Its core code is as follows : public final class Integer {

private final int value;

}

The two one. Integer When comparing examples, pay special attention to : Absolutely not == Compare , because Integer Is a reference type , You have to use equals() Compare :

        Integer x = 127;
        Integer y = 127;
        Integer m = 99999;
        Integer n = 99999;
        System.out.println("x == y: " + (x==y)); // true
        System.out.println("m == n: " + (m==n)); // false
        System.out.println("x.equals(y): " + x.equals(y)); // true
        System.out.println("m.equals(n): " + m.equals(n)); // true

{card-list-item} Integer.valueOf() May always return the same Integer example , therefore , In our own creation Integer When , There are two ways : Method 1:Integer n = new Integer(100); Method 2:Integer n = Integer.valueOf(100); Method 2 Better , Because of the method 1 Always create new Integer example , Method 2 Leave internal optimization to Integer The implementer of , Even if there is no optimization in the current version , It is also possible to optimize in the next version . We can create “ new ” The static method of an object is called a static factory method .Integer.valueOf() Is the static factory method , It returns cached instances as much as possible to save memory .

When creating a new object , The static factory method is preferred to new The operator .

{/card-list-item}

{/tabs-pane} {tabs-pane label=" Hexadecimal conversion "}

System.out.println(Integer.toString(100)); // "100", Expressed as 10 Base number 
System.out.println(Integer.toString(100, 36)); // "2s", Expressed as 36 Base number 
System.out.println(Integer.toHexString(100)); // "64", Expressed as 16 Base number 
System.out.println(Integer.toOctalString(100)); // "144", Expressed as 8 Base number 
System.out.println(Integer.toBinaryString(100)); // "1100100", Expressed as 2 Base number 

The output is String, In computer memory , Only binary representation , There is no decimal or hexadecimal representation .int n = 100 Always use... In memory 4 Binary representation of bytes Frequently used System.out.println(n); It relies on the core library to automatically format integers into 10 Hexadecimal output and display on the screen , Use Integer.toHexString(n) Then the integer is automatically formatted as 16 Base number . Notice an important principle of programming : The storage and display of data should be separated .

{/tabs-pane} {tabs-pane label=" Handle unsigned integers "}

public class Main {
    public static void main(String[] args) {
        byte x = -1;
        byte y = 127;
        System.out.println(Byte.toUnsignedInt(x)); // 255
        System.out.println(Byte.toUnsignedInt(y)); // 127
    }
}

{/tabs-pane}

This article altogether 721 Number of words , Average reading time ≈ 2 minute

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[await]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/09/20210909125824179E.html

Scroll to Top