Although the Java language is a typical object-oriented programming language, eight of the basic data types do not support object-oriented programming, and the basic types of data do not have "object" attributes-no attributes, no methods to invoke. They are used only to cater to the deep-rooted habits of human beings and indeed to perform routine data processing in a simple and effective manner.
This use of non-object-oriented technology can sometimes be inconvenient, such as reference type data inherit the attributes of the object class, to be converted to a String type (which is often required) simply call the ToString () defined in the object class, and the base data type is converted to The String type is much more cumbersome. To solve this problem, Java has designed the corresponding class for each basic data type, called the wrapper class (Wrapper Classes), and the textbook is called the class of the outer or data type.
Basic data types and corresponding wrapper classes
Basic data Types |
the corresponding packaging class |
Byte |
Byte |
Short |
Short |
Int |
Integer |
Long |
Long |
Char |
Character |
Float |
Float |
Double |
Double |
Boolean |
Boolean |
The objects of each wrapper class can encapsulate a corresponding base type of data and provide some other useful methods. Once a wrapper class object is created, its contents (the data value of the underlying type being encapsulated) are immutable.
The basic types and corresponding wrapper classes can be swapped with each other:
- The conversion from the basic type to the corresponding wrapper class is called boxing, such as wrapping int as an object of an Integer class;
- The wrapper class translates to the corresponding base type as a unboxing, such as simplifying the object of the Integer class to int.
Application of packaging class eight packaging classes are similar in use, the following are common application scenarios. 1) the conversion between int and integer can be boxed int by the method of constructing the integer class, and the integer is removed by the Intvalue method of the integer class. For example:
1 Public classDemo {2 Public Static voidMain (string[] args) {3 intm = 500;4Integer obj =NewInteger (m);//Manual Boxing5 intn = obj.intvalue ();//Manual Unpacking6System.out.println ("n =" +n);7 8Integer obj1 =NewInteger (500);9System.out.println ("obj equivalent to obj1?") " +obj.equals (obj1));Ten } One}
Operation Result:
n = 500
is obj equivalent to obj1? True2) Converts a string to an integer class has a static Paseint () method that converts a string to an integer with the following syntax:
1 int radix);
S is the string to convert, radix is binary, optional, default is decimal.
The following code will tell you what kind of string can be converted to an integer:
1 Public classDemo {2 Public Static voidMain (string[] args) {3String str[] = {"123", "123abc", "abc123", "abcxyz"};4 5 for(String str1:str) {6 Try{7 intm = Integer.parseint (str1, 10);8System.out.println (str1 + "can be converted to integers" +m);9}Catch(Exception e) {TenSystem.out.println (str1 + "cannot be converted to integers"); One } A } - } -}
Operation Result:
123 can be converted to integers 123
123ABC cannot be converted to an integer
Abc123 cannot be converted to an integer
ABCXYZ cannot be converted to an integer 3) converting an integer to a string the integer class has a static toString () method that converts an integer to a string. For example:
1 Public class Demo {2 Public Static void Main (string[] args) {3 int m =N; 4 String s = integer.tostring (m); 5 System.out.println ("s =" + s); 6 }7 }
Operation Result:
s = 500 automatic unpacking and boxing the above example requires manual instantiation of a wrapper class called Manual unboxing. Java 1.5 (5.0) must be manually unboxing before unpacking.
Java 1.5 can be automatically unboxing, that is, in the basic data types and corresponding packaging class conversion, the system will automatically, which will greatly facilitate the programmer's code writing. For example:
1 Public classDemo {2 Public Static voidMain (string[] args) {3 intm = 500;4Integer obj = m;//Automatic Boxing5 intn = obj;//Automatic Unpacking6System.out.println ("n =" +n);7 8Integer obj1 = 500;9System.out.println ("obj equivalent to obj1?") " +obj.equals (obj1));Ten } One}
Operation Result:
n = 500
is obj equivalent to obj1? True
Automatic unboxing is a common feature that needs to be mastered.
Series Articles:
Java know how much (1) Language overview
Java know how much (2) virtual machine (JVM) and cross-platform principle
Java know how much (3) employment direction
Java know how much (4) the difference between J2SE, Java EE, J2ME
Java know how much (5) Build Java development environment
Java know how much (6) The first example of a program
Java knows how many (7) classes and objects
Java know how much (8) class library and its organizational structure
Java know how much (9) Import and Java class search path
Java know how much (10) data types and variables
Java know how much (11) data type conversions
Java know how many (12) operators
Java know how much (13) Process Control
Java know how many (14) arrays
Java know how much (15) string
Java know how much (StringBuffer) and Stringbuider
Java know how much (17) emphasize the programming style
Java know how many (18) classes are defined and instantiated
Java know how many (19) access modifiers (access control characters)
Java knows how many (20) variables are scoped
Java know how much (+) This keyword is detailed
Java know how many (22) method overloads
Java know how much (23) the basic run order of classes
Java know how much (24) packaging class, unpacking and packing detailed
Java know how much (24) packaging class, unpacking and packing detailed