1. Introduction of Packing class
Java is an object-oriented computer language but not completely object-oriented, 8 basic data types are not object-oriented, but Java gives us 8 kinds to create objects for these basic data types, which are collectively referred to as wrapper Classes. Specific as Follows:
The 6 categories except the Boolean and Char classes form part of the parent class Number.
2. Why use a wrapper class
Wrapper classes can use objects to greatly enrich the operation of basic types of data, the most common is the conversion of the binary and string conversion to the basic data type. however, because the wrapper class is used to create objects, space is created in the heap memory, and the base data type is only stuck in the stack memory, so it is more efficient and resource-intensive than the basic type data, so there is no need to create wrapper classes to create wrapper Classes.
3. Automatic loading and unpacking
After jdk5.0 the introduction of automatic loading and unpacking to facilitate the writing, specifically as Follows:
1 packagewrapper;2 3 public classNew4 {5 public Static voidmain (string[] Args)6 {7Double d1=NewDouble (3.14);//Standard Form. 8Double d2=3.14;//automatic boxing, Compile-time compiler automatically modified to the form above and then Create. 9 DoubleD3=d1.doublevalue ();//Standard Form. Ten Doubled4=d1;//Automatic Unpacking. one } a}
4. Common method of packing class
1 packagewrapper;2 3 public class first4 {5 public Static voidmain (string[] Args)6 {7String a= "1234";8 intB=integer.parseint (a);//converting a string to a base data type9System.out.println (b+10);Ten intc=0723; oneString d= "" +c; aSystem.out.println (integer.parseint (d, 10));//The binary conversion, returned as the base data type, preceded by the number of pre-conversion (can be a character), and the subsequent argument is why the conversion is Binary. -Integer w=integer.valueof (c);//converting a basic data type to a wrapper class - intm=W.intvalue (); the System.out.println (m); -String s=integer.tobinarystring (c);//the binary conversion, returned as a string. This is converted to 2, tooctalstring is 8, and tohexstring is 16 binary. - System.out.println (s); -System.out.println (integer.compare (b, c));//you can directly compare the basic data type, return 0 for equality, and return a large 1. small return-1. +Double y=NewDouble (3.14); -Double x=NewDouble (2.14); +System.out.println (x.compareto (y));//you can also compare wrapper class objects a } at}
A brief analysis of packaging classes in Java