To compensate for the lack of basic data types in object-oriented terms, the Java language introduces encapsulation classes (wrapper Classes), which provide the appropriate reference data types for a variety of basic data types.
Encapsulated classes are defined as final, so they cannot be inherited, and once created, the content cannot be changed.
public class Demo1 {public
static void Main (string[] args) {
byte byte1 = new Byte ((Byte) 1);
NumberFormatException
Integer i = new Integer ("a");
System.out.println (i.ToString ());
System.out.println (I.intvalue ());//int
Boolean flag = new Boolean ("true")
; SYSTEM.OUT.PRINTLN (flag);
Java.lang.NumberFormatException
String s= "100s";
Object-oriented
Double d = double.valueof (s);
Base data type
double DD = double.parsedouble (s);
}
}
the construction methods for each wrapper class have several overloaded forms, which are summarized as follows:
All encapsulation classes can construct their instances using the corresponding base type data as parameters. For example:
Boolean Bln=new Boolean (true); BYTE b=new byte (byte 1);
In addition to the character class, other wrapper classes can construct their instances with strings as arguments. For example:
Integer I =new integer ("123"); Double D=new double ("123.45D");
When the parameter of the constructor of a Boolean class is a string type, if the string content is true (regardless of case), the Boolean object represents true, otherwise it represents false. For example:
System.out.println (Newboolean ("true")); Print true System.out.println (new Boolean ("AAAA")); Print False
When the arguments for the construction method of Byte, short, Integer, Long, float, and double are string, the string cannot be null, and the string must be resolvable to the corresponding base type of data, or, if the compilation passes, The runtime, however, throws a NumberFormatException run-time exception.
The Java language uses encapsulated classes to convert basic type data to objects. Each Java base type has a corresponding encapsulated class in the Java.lang package.