Java core class - Package Type

Java are two types of data:
  basic types: byte, short, int, long , boolean, float, double, char
  reference types: class, interface

Reference types can be assigned to null, meaning empty, but the basic types can not be assigned null:

String s = null;
int n = null;  //comile error!

How, then, to a primitive type as an object (reference type)?
For example, you want to int primitive type into a reference type, we can define an integer class,
it contains only one instance field int, so, Integer class can be seen as an int packaging (Wrapper class):

public class Integer {
    private int value;
    
    public Integer(int value) {
        this.value = value;    
    }
    
    public int intValue() {
        return this.value;    
    }
}

Integer definition of a good class, we can put int and Integer mutual conversion:

Integer n = null;
N2 Integer = new new Integer (99)   // converts int Integer 
int N3 = n2.inValue ();   // to convert integer int

In fact, because the packaging is very useful, Java core libraries for each basic type provides a corresponding packaging type:

We can directly use, you do not need to define himself:

public  class catchExample2 {
     public  static  void main (String [] args) {
         int I = 100 ;
         // Create a new operator by Integer instance, in this way, there will be compiler warnings; 
        Integer N1 = new Integer (I);
         //   you can be created by the static method valueOf examples Integer; 
        Integer N2 = Integer.valueOf (I);
         //   create a static method Integer valueOf (String) are possible; 
        Integer Integer.valueOf N3 = ( "100" );
        System.out.println(n3.intValue());
    }
}

Because int and Integer can be converted to each other:

int i = 100;
The n-Integer = Integer.valueOf (i);
 int the X-n.inValue = ();   //
 So, Java compiler can help us to automatically transition between int and Integer:
N-Integer = 100;   // compiler automatically Integer.valueOf (int) 
int X = n-;   // compiler automatically Integer.inValue ()

This goes directly to the assignment of writing int Integer, called autoboxing (Auto Boxing),
in turn, becomes the Integer int writing assignment, called auto-unboxing (Auto Unboxing).
Note that the automatic boxing and unboxing only occurs automatically at compile time, the purpose is to reduce code.

Packing and unpacking will affect the efficiency of the code, because the code is compiled class strict distinction between primitive types and reference types.
And it may report NullPointerExecption executed automatically when unpacking.

All packaging types are the same class.

public final class Integer {
    private final int value;
}

Therefore, once the Integer object is created, the object is the same.

When we create integer, there are two methods:

Integer n = new Integer(100);
Integer n = Integer.valueOf(100);

The former always creates a new object, which method is better.

We will be able to create a new object of a static method called static factory method,
Integer.valueOf () is a static factory method that returns the cache instances as much as possible to save memory.

Integer class itself provides a number of methods, e.g., static method most frequently used the parseInt () can parse the string into an integer:

int X1 = the Integer.parseInt ( "100"); // 100 
int X2 = the Integer.parseInt ( "100", 16); // 256, as resolved by hexadecimal

Integer integer format may also be specified to be hexadecimal string:

public class Main {
    public static void main(String[] args) {
        System.out.println (Integer.toString ( 100)); // "100", expressed in decimal 
        System.out.println (Integer.toString (100, 36)); // "2S", expressed as 36 ary 
        System.out.println (Integer.toHexString (100)); // "64", represented as hexadecimal 
        System.out.println (Integer.toOctalString (100)); // "144", as represented 8 ary 
        System.out.println (Integer.toBinaryString (100)); // "1100100", represented as binary 
    }
}

 

Guess you like

Origin www.cnblogs.com/yangmingxianshen/p/12501378.html
Recommended