Objective
As mentioned earlier, except int,float these basic data types, all other data types are classes in Java.
So, what if I want these basic types to be class types, too?
Then use the object wrapper.
The role of the wrapper
1. It can provide many types of transformation-related approaches.
2. The members of the generic data can only be objects.
Automatic Boxing
When you use an underlying type, but the actual type requirement is its wrapper, the compiler does not give an error and will help you automate the transformation.
Such as:
ArrayList <Integer> array = new ArrayList <> ();
Array.add (3);
Before the Add method is executed, the base type 3 is converted to the integer wrapper type.
code example
1 Packagetest;2 3 Public classJava7learn {4 5 Public Static voidMain (string[] args) {6 7 /*8 * Integer conversion to String type9 */Ten inti = 999; One //defines an integer object and initializes it with an I value AInteger tempi =i; - //converting an Integer object to a String type - tempi.tostring (); the - //of course, you can also use the following static method to convert directly - //integer.tostring (i); - + /* - * String type converted to integral type + */ AString s = "1"; at //converting a string type to an integral type - Integer.parseint (s); - } -}
Summary
It's a real hassle to recall the process of implementing object transformations (involving string types) in C + + before.
Java provides encapsulation of the underlying data, which naturally makes it more powerful and easier to use.
Object Wrapper and Auto Boxing