java wrapper class
The content of the article is selected from Shang Silicon Valley
Packaging introduction
The basic data type in java is not a kind of class, there is no method, and there is no attribute, while java is object-oriented programming. If the basic data type is also regarded as a kind of class, and then the methods and attributes of the class are called, then Java can fully reflect the idea of object-oriented programming.
To solve this problem, java provides tools for packaging classes.
Picture from Shang Silicon Valley
As shown in the figure, each packaging class type corresponds to a basic data type one by one. Except for the packaging classes of boolean and char, the rest of the parent classes are all Number classes. Basic data types and packaging types are two independent tools, which can be converted to each other. In programming, you can use wrapper classes to replace basic data types to solve the problem that basic data types cannot call methods.
Conversion between packaging classes and basic data types and strings
A picture can be used to summarize the conversion relationship. The
picture is selected from Shang Silicon Valley
Conversion between packaging classes and basic data types
Convert basic data types into packaging classes
Methods unit testing, packaging and testing the basic data type to
format packaging wrapper class variable name = new (initial value);
following code, create a class WrapperTest
package com.atguigu.java2;
import org.junit.Test;
public class WrapperTest {
@Test
public void test1(){
int num1 = 10;
Integer in1 = new Integer(num1);
System.out.println(in1 );
System.out.println(in1.toString() );
System.out.println(in1 + 1);
System.out.println(in1.toString()+1);
}
}
The test result is
10
10
11
101
Analyze the reasons for these four results. The first is because of the println method. When the parameter list is a reference to an object, the toString method will be called. Because the wrapper class rewrites the toString method, the output is 10. For details, please refer to Java Object Class methods (equals and toString) . The
second is to directly call the toString method of the wrapper class, the principle is the same as the first.
The fourth is to test whether the result returned by toString is an integer or a string type. It can be seen that the string type is returned. The
third should be the same as the fourth in principle, but there is an automatic unboxing of java. The expression is first converted to The result of the integer type is then output.
You can also put strings in the constructor
Integer in2 = new Integer("123");
System.out.println(in2.toString());
System.out.println(in2.toString()+1);
The result is
123
1231
However, if it is changed to Integer in2 = new Integer("123a"); Although the compilation will pass, the operation will report an error java.lang.NumberFormatException
In particular, for Boolean types
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean("true");
System.out.println(b1);
System.out.println(b2);
Boolean b3 = new Boolean("true122");
Boolean b4 = new Boolean("truE");
System.out.println(b3);
System.out.println(b4);
operation result
true
true
false
true
When the value of the string is not true, the output will report false instead of an error, and the value of the string is not case sensitive.
Analyze the constructor of Boolean ("true122")
public Boolean(String s) {
this(parseBoolean(s));
}
Click parseBoolean to view the source code
public static boolean parseBoolean(String s) {
return ((s != null) && s.equalsIgnoreCase("true"));
}
It can be seen that equalsIgnoreCase means case-insensitive, if the result of case-insensitive is not "true", return false.
Continue to write a class in the java file of WrapperTest
class Order{
boolean isMale;
Boolean isFemale;
}
Test again
Order order = new Order();
System.out.println(order.isMale);
System.out.println(order.isFemale);
Test Results
false
null
Because Boolean is a class and isFemale is a reference data type, the value of the reference data type is either null or an address value, because the object has not been created yet, so it is null.
Convert packaging class to basic data type
Method: call the xxxValue() method of the wrapper class. The
code is as follows
@Test
public void test2(){
Integer in1 = new Integer(12);
int i1 = in1.intValue();
System.out.println(i1);
}
The test result is 12
Auto-boxing and auto-unboxing
Auto-boxing and auto-unboxing are new features added after JDK5.0.
Automatic boxing is to convert basic data types into packaging types, application scenarios
@Test
public void test3(){
int in1 = 13;
method(in1);
Integer num = 14;
Integer num2 = 15;
System.out.println(num.toString());
System.out.println(num2.toString());
}
public void method(Object obj){
System.out.println(obj);
}
The result is
13
14
15
When the method method is run, the formal parameters of the method are classes. It is reasonable to say that the basic data types cannot be put in, but automatic boxing will automatically convert the basic data types into packaging classes according to requirements.
Automatic unboxing is to convert the packaging class into a basic data type.
Integer num = 14;
int i1 = num;
System.out.println(i1);
This is an automatic assembly and unpacking process, and the output result is 14.
Conversion between packaging class and basic data type with String class
With automatic loading and unboxing, packaging classes and basic data types can be regarded as a whole, and now we will study the conversion between this whole and String.
Basic data types and packaging classes are converted to String type
Method 1: Convert by connecting operation
int num1 = 20;
String str = num1+ "";
Method 2: Call the valueOf(Xxx xxx) method of String overload.
@Test
public void test4(){
Float f1 = 12.3f;
String str = String.valueOf(f1);
System.out.println(str);
}
String type is converted to basic data type or packaging class
The
code for calling the parseXxx method in the wrapper class is as follows
@Test
public void test5(){
String str1 = "123";
// Integer in1 = (Integer)str1;
int i = Integer.parseInt(str1);
System.out.println(i);
}
The result of the operation is 123, which shows that the conversion between String and the packaging class must call the method of the converted type, that is, the method in the class on the left side of the equal sign.
For boolean type, if you add other characters to the string data on the right side of the equal sign, the conversion result is false. If it is not boolean, the conversion will report an error.
ps: For the commented statement, the compiler will report an error, because the use of forced conversion can only occur when there is a child-parent relationship. If there is no child-parent relationship, the compiler will report an error.
Picture from Shang Silicon Valley