Basic type wrapper class overview
In the actual program use, the user input data on the program interface is stored as a string type. In the program development, we need to convert the string data to the specified basic data type according to the requirement, such as the age needs to be converted into int type, the test result needs to be converted into double type, etc. So, what about converting between strings and basic data?
The corresponding object is provided in Java to solve the problem, the basic data type Object wrapper class: Java encapsulates the base data type value into an object. What are the benefits of encapsulating objects? The ability to provide more basic values for operations.
The packaging classes for the 8 basic types are as follows:
byte--byte
Short->short
int, Integer
Long->LOMG
Double->double
Float->float
Char->character
Boolean->boolean
It should be noted that int corresponds to an Integer, char corresponds to the Character, the other 6 are the basic type of the first letter uppercase .
Basic data type Object wrapper class features: Used to convert between basic data and strings .
To convert a string to a base type:
Parsexxx (string s), where xxx represents the base type, the argument is a string that can be converted to the base type, and if the string cannot be converted to the base type, a problem with the number conversion will occur numberformatexception
System.out.println (integer.parseint ("123") + 2);
Print result is 125
There are 3 ways to convert a base value to a string:
The basic type is directly connected with "";34+ ""
Call the ValueOf method of String;string.valueof ;
Call the ToString method in the wrapper class;integer.tostring ;
Basic types and Object conversions
Using the int type to demonstrate with integer object conversions, the other basic types are converted in the same way.
Basic values----> Wrapper objects
Integer i = new Integer (4);//Use constructor function
Integer II = new Integer ("4");//The constructor can pass a numeric string
Integer III = integer. ValueOf(4);//Use the ValueOf method in the wrapper class
Integer IIII = integer. ValueOf("4");//Use the ValueOf method in the wrapper class
Package object----> Basic values
int num = I.intvalue ();
Automatic packing and unpacking
The base type and the wrapper type can be generic if required. Sometimes when we have to use reference data types, we can pass in the base data type.
Like what:
The base type can be evaluated directly using an operator, but the reference type is not available. The basic type wrapper class, as one of the reference types, can be computed because Java "secretly" automatically transforms an object into a basic data type.
Correspondingly, the value of the reference data type variable must be the memory space address value of new, and we can assign a value of a base type to a reference to a basic type wrapper class. The same reason is that Java also "secretly" automatically transforms the basic data type to the object.
L Auto-Unpacking: Objects automatically turn directly into basic values
L Auto-Boxing: Basic values automatically turn directly into objects
Integer i = 4;//Auto-boxing. equivalent Integer i = integer.valueof (4);
i = i + 5;//equals to the right: Converts the I object to the base value (auto-unpacking) I.intvalue () + 5; After the addition operation is finished, it is boxed again , and the basic values are converted to objects.
L Auto-Boxing (byte Chang) details demo
when the value is within the byte range, auto-boxing does not create the new object space but uses the existing space.
Integer a = new integer (300);
Integer b = new integer (300);
System. out. println (a==b);//false
System. out. println (a.equals (b));//true
System. out. println ("---------------------");
Integer x = 127;
Integer y = 127;
//When the jdk1.5 is boxed automatically, if the value is within the byte range, the object space is not newly created, but the original space is used .
System. out. println (x==y); True
System. out. println (X.equals (y)); True
Overview and conversion of basic data types and wrapper classes in Java