Package Type 22-

Package Type 
Java are two types of data: 
basic types: byte , Short , int , Long , boolean, float , Double , char 
reference type: All class and interface types 

reference types can be assigned to null, meaning empty, but the basic types can not assigned to null. 
Java core libraries are provided for each basic type corresponding to the type of packaging 

refers to the corresponding basic type type 
boolean java.lang.Boolean 
byte java.lang.Byte
 Short java.lang.Short
 int java.lang.Integer
 Long the java.lang. Long
 float java.lang.Float
 Double java.lang.Double
 char java.lang.Character 
can be used as
public class{The Main public static void main (String [] args) { int I = 100 ; // Create instance Integer (not recommended, there compiler warnings) by the new operator: Integer N1 = new Integer (I); // by static method valueOf (int) Create Integer example: Integer = N2 Integer.valueOf (I); // Create instance Integer static method valueOf (String): Integer = N3 Integer.valueOf ( " 100 " ); . the System OUT .println (n3.intValue ()); } } because int Integer and can be converted to each other: int I = 100; Integer n- = Integer.valueOf (I); int X = n.intValue (); therefore, Java compiler helps automatically transition between int and Integer: Integer n- = 100 ; // compiler automatically Integer.valueOf (int) int X = n-; // compiler automatically Integer.intValue () which goes directly to the assigned writing int Integer called autoboxing (Auto boxing), in turn, becomes the Integer of int written assignment, called auto-unboxing (Auto unboxing). Note: auto-boxing and auto-unboxing occur only at compile time, the purpose is to write less 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 may be reported automatically unpacking NullPointerException performed: public class the Main { public static void main (String [] args) { Integer n- = null; Int I = n-; } } same class all packaging types are the same class. We see Integer source code shows that its core code is as follows: public Final class Integer { Private Final int value; } Note: Once you have created Integer object, the object is the same. Integer compare two instances must not use == , because Integer is a reference type, you must use equals () comparison. Base conversion Integer class itself provides a number of methods for binary conversion. For example: // static method 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 format for the specified hexadecimal string public class the Main { public static void main (String [] args) { the System. OUT .println (Integer.toString ( 100 )); // "100", represented as decimal the System. OUT .println (Integer.toString ( 100 , 36 )); // "2S", represented as hexadecimal 36 the System. OUT .println (Integer.toHexString ( 100 )); // "64", expressed in hexadecimal the System. OUT .println (Integer.toOctalString (100 )); // "144", as represented by octal the System. OUT .println (Integer.toBinaryString ( 100 )); // "1100100", represented as binary } } Note: The above-described method is output String, in the computer's memory, only expressed in binary, decimal or hexadecimal representation does not exist. Processing an unsigned integer in Java, no signed or unsigned integer (Unsigned) of the basic data types. byte , Short , int and long are unsigned integer most significant bit is the sign bit. Unsigned int and signed integer conversion in Java needs to use the static method to complete the package type. // put a negative byte unsigned integer is converted int public class the Main { public static void main (String [] args) { byte X = - . 1 ; byte Y =127 ; the System. OUT .println (Byte.toUnsignedInt (X)); // 255 the System. OUT .println (Byte.toUnsignedInt (Y)); // 127 } } Because the byte - in binary representation is 1 11111111 to unsigned int int after conversion is 255. Similarly, it can be converted in a short unsigned int, and unsigned converter according to an int to a long.

 

Guess you like

Origin www.cnblogs.com/nsss/p/11417616.html