Why use packaging class
Although Java is an object-oriented programming language, it contains eight basic data types, which do not support the object-oriented programming mechanism.
The data of these basic data types does not have the characteristics of "object", such as no property and no method can be called. These eight basic data types bring some convenience, such as simple and effective conventional data processing.
However, there are some restrictions when using basic data types. For example, all variables of reference types inherit the Object class and can be used as Object type variables. But variables of basic data type can't be used. If there is a ten thousand method that needs parameters of Object type, but the actual required value is a number such as the number 3, it may be difficult to deal with.
In order to solve the problem that variables of eight basic data types cannot be used as Object variables, a wrapper class is introduced in Java. Through the wrapper class, corresponding reference types can be defined for eight basic data types, which is called the wrapper class of basic data types.
Packaging type
The Java library provides eight classes in the java.lang package to represent each of the eight basic types.
These classes are called wrapper classes because they wrap the original value in the object.
The following table lists the original types and their corresponding wrapper classes.
Basic types | Packaging class |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
Method
All packaging classes are immutable. They provide two ways to create their objects:
- Using constructors
- Using the valueOf() factory method
In addition to Character, each wrapper class provides at least two constructors: one takes the value of the corresponding primitive type, and the other takes String.
The Character class provides only one constructor that accepts a Character.
The following code creates objects for some wrapper classes:
public class Main { public static void staticMethod() { // Creates an Integer object from an int Integer intObj1 = new Integer(100); // Creates an Integer object from a String Integer intObj2 = new Integer("1969"); // Creates a Double object from a double Double doubleObj1 = new Double(10.45); // Creates a Double object from a String Double doubleObj2 = new Double("234.60"); // Creates a Character object from a char Character charObj1 = new Character("A"); // Creates a Boolean object from a boolean Boolean booleanObj1 = new Boolean(true); // Creates Boolean objects from Strings Boolean booleanTrue = new Boolean("true"); Boolean booleanFalse = new Boolean("false"); } }
valueOf()
Another way to create wrapper class objects is to use their valueOf() method.
The valueOf() method is static.
The following code uses their valueOf() method to create objects of some wrapper classes:
public class Main { public static void staticMethod() { Integer intObj1 = Integer.valueOf(100); Integer intObj2 = Integer.valueOf("2014"); Double doubleObj1 = Double.valueOf(10.45); Double doubleObj2 = Double.valueOf("234.56"); Character charObj1 = Character.valueOf("A"); } }
Instance file baozhuang.java
public class baozhuang { public static void main(String[] args) { boolean bl = true; //The basic type variable of b1 is wrapped as a packing object by a constructor Boolean blObj = new Boolean(bl); int it = 5; //it basic type variables are wrapped as wrapper class objects through constructors Integer itObj = new Integer(it); //Convert a string to a Float object Float fl = new Float("1.23"); //Convert a string to a Boolean object Boolean bObj = new Boolean("trUe"); System.out.print("---------" + bObj); //java.lang.NumberFormatException exception will be thrown below //Long lObj = new Long("ddd"); //Take out the boolean variable in the Boolean object boolean bb = bObj.booleanValue(); //Take the int variable from the Integer object int i = itObj.intValue(); //Take out the float variable in the float object float f = fl.floatValue(); } }
In the example code above, it shows how to convert basic type variables into corresponding wrapper class objects, and how to wrap a string into wrapper class objects.
In the above program, basic type variables such as e,5 can be packaged as wrapper class objects.
By passing a string parameter to the wrapper class constructor, the wrapper class objects are created with strings such as "4.56" and "false".
One of the lines tries to change the string "ddd" to a Long type variable, so there is no problem at compile time, but java.lang.NumberFormat will be raised at run time
Exception exception