1, Packaging class overview
· The advantage of encapsulating a basic data type as an object is that more functional methods can be defined in the object to manipulate the data.
· One of the most common operations: for converting between basic data types and strings .
· Correspondence of basic types and wrapper classes
In order to perform more operations on the basic data types and to operate more conveniently, Java provides the corresponding class type for each of the basic data types. The wrapper class type. ByteByteShort ShortIntIntegerLongLongFloatFloatDoubleDoubleCharCharacterBooleanBoolean2. Integer class
/*** Method of constructing integer: * Public integer (int value) * Public integer (string s) * Note: This string must be made up of numeric characters*/ Public classintegerdemo01{ Public Static voidMain (string[] args) {//Way One: intI =100; Integer II=NewInteger (i); System.out.println ("II:" +II);//ii:100//Way Two:String s = "100"; Integer III=NewInteger (s); System.out.println ("III:" +III);//iii:100 }}
Member Methods:
Public classIntegerDemo02 { Public Static voidMain (string[] args) {//output 100 binary, octal and hexSystem.out.println (integer.tobinarystring (100));//1100100System.out.println (integer.tooctalstring (100));//144System.out.println (integer.tohexstring (100));// -System.out.println (Integer.max_value);//2147483647System.out.println (Integer.min_value);//-2147483648 }}
3, int type and string type of mutual conversion
· public int intvalue (): Returns the value of the integer as type int •public static int parseint (string s): Parses a string argument as a signed decimal integer· public static string toString (int i): Returns a String object that represents the specified integer · public static IntegervalueOf(int i): Returns an integer instance that represents the specified int value · public static integer valueOf (String s): Returns an integer instance representing the specified String value
//INT--String:String.valueOf (int)//string--Int:Integer.paraseInt (string) Public classIntegerDemo03 { Public Static voidMain (string[] args) {//int --String intnum = 100; //Way One:String S1 = "" +num; System.out.println ("S1:" +S1); //Way two: recommendedString s2 =string.valueof (num); System.out.println ("S2:" +S2); //Way Three://------and StringString s3 =NewInteger (num). toString (); System.out.println ("S3:" +S3); //mode four:String S4 =integer.tostring (num); System.out.println ("S4:" +S4); System.out.println ("----------------"); //String --INT//mode one: String ------and int//public int Intvalue ();String s = "100"; Integer I=NewInteger (s); intI1 =I.intvalue (); System.out.println ("I1:" +i1); //Way Two: Integer.paraseint (s): Recommended intI2 =Integer.parseint (s); System.out.println ("I2:" +i2); }}4. Common basic ConversionA: Decimal into binary, octal and hexadecimal
public static String tobinarystring (int i) public static String tooctalstring (int i) public static String tohexstring (int i) B: Decimal to another binary public static String toString (int i,int radix) Range is from 2 to 36
C: Other binary to decimal public static int parseint (String s,int Radix)
Public classIntegerDemo04 { Public Static voidMain (string[] args) {//decimal to binary, octal, hexSystem.out.println (integer.tobinarystring (100));//1100100System.out.println (integer.tooctalstring (100));//144System.out.println (integer.tohexstring (100));// -//decimal to another binarySystem.out.println (integer.tostring (100, 10));// -System.out.println (integer.tostring (100,2));//1100100System.out.println (integer.tostring (100,36));//2sSystem.out.println (integer.tostring (100, 37));// -//other binary to decimalSystem.out.println (Integer.parseint ("100", 10));// -System.out.println (Integer.parseint ("100", 2));//4//error, because there is no 2,3 in binary//System.out.println (Integer.parseint ("123", 2)); }}5. New features of JDK5:Automatic Boxing: Turn the basic type into a wrapper class type
Automatic Unpacking: Turn wrapper class types into basic typesinteger x = new Integer (4), which can be written directlyInteger x = 4;//Auto-boxing. x = x + 5;//automatic unpacking. Through the Intvalue method. need to note: when used, Integer x = null; The above code appears nullpointerexception.
Public classIntegerDemo05 { Public Static voidMain (string[] args) {//defines a wrapper class type variable i for an int type//integer i1 = new Integer (+);Integer i2 = 200; I2+ = 300; System.out.println ("I2:" +i2); //through the post-compilation code//Integer i2 = integer.valueof (+);//Automatic Boxing//i2 = integer.valueof (I2.intvalue () + 200); //System.out.println ((New StringBuilder ("I2:")). Append (I2). toString ()); }}6, Integer direct assignment of the face questionNote: Integer data is directly assigned, and if between 128 and 127, the data is directly fetched from the buffer pool.
Public classIntegerDemo06 { Public Static voidMain (string[] args) {Integer i1=NewInteger (127); Integer I2=NewInteger (127); System.out.println (I1= = I2);//falseSystem.out.println (I1.equals (I2));//trueSystem.out.println ("---------------"); Integer i3=NewInteger (128); Integer I4=NewInteger (128); SYSTEM.OUT.PRINTLN (i3= = i4);//falseSystem.out.println (I3.equals (I4));//trueSystem.out.println ("---------------"); Integer i5= 128; Integer I6= 128; SYSTEM.OUT.PRINTLN (i5= = I6);//falseSystem.out.println (I5.equals (I6));//trueSystem.out.println ("---------------"); Integer i7= 127; Integer i8= 127; SYSTEM.OUT.PRINTLN (i7= = i8);//trueSystem.out.println (I7.equals (i8));//trueInteger i = integer.valueof (127); //by looking at the source code, a data buffer pool is made for data between 128 and 127 .//If a data is within that range, each time it does not create a new space,//if not inside this range, a new object will be created.//Public static Integer valueOf (int i) {//assert Integercache.high >= 127; //if (i >= integercache.low && i <= integercache.high)//return integercache.cache[i + (-integercache.low)]; //return new Integer (i); }}
Java api--Wrapper class