|
You can create wrapper objects by either of the following two ways:
Use the wrapper constructor that takes a primitive value as argument.
Invoke a static valueOf() method of a corresponding wrapper class. This method takes a String representation of a primitive as an argument and returns a wrapper object.
If you want a particular primitive value, say int value 45 to be treated as object, you need to wrap it into a wrapper object. You can create a wrapper object by following these steps :
1. Find out what is the corresponding wrapper class for this primitive type. For int primitive, it will be Integer class.
2. Create an instance of the wrapper class by passing the int value as:
Integer intObj = new Integer(45);
Now once the intObj wraps the value 45, it will always hold this value. You can create a wrapper object from any primitive value like this. All of the wrapper classes except Character class provide two constructors: - one that takes a primitive of the type being constructed, and one that takes a String representation of the value that is being wrapped. For example, you can create the primitive wrapper object as:
Integer i1 = new Integer(42); // using the primitive value as argument
Integer i2 = new Integer("42"); // using the string
representation as
// argument
Float f1 = new Float(3.14f);
Float f2 = new Float("3.14f");
The wrapper class Character has only one constructor that takes a char value as argument. For instance,
Character c1 = new Character(‘c’);
Character c2= new Character("c"); // Compiler error!
Using the static valueOf() methods that take String arguments
Another way of creating a wrapper object is invoking a static method valueOf() method of a wrapper class. This method takes a string as an argument.
valueOf(String s)
If you want to get a wrapper object for int value 45, you can do so by following these steps:
1. Find out what is the corresponding wrapper class for this primitive type. For int primitive, it will be Integer class.
2. Invoke Integer.valueOf() method by passing a string containing the int value as:
Integer intObj = Integer.valueOf(“45”);
Note that this method throws a runtime exception (NumberFormatException) when the string passed as argument cannot be converted into the appropriate primitive. For example, if you invoke this method as Integer.valueOf(“Forty five”), it will throw exception as it cannot convert “Forty five” into 45. Remember that you must pass the numeric representation as a string (say “45”) to this method. The textual representation is not accepted.
|
|
All wrappers except Character have the valueOf(String) method. |
valueOf(String s, int radix)
There is another version of valueOf method that takes two arguments. The first argument is a string representation of the appropriate type of primitive. The second argument is a integer value radix that indicates the number system in which the first argument is represented. For instance, when a radix 2 is passed, the valueOf() method assumes that the string representation is in binary number system. Thus radix value 2,8,and 16 correspond to binary, octal and hexadecimal systems respectively.
The number system indicated by radix will be used while converting the value passed as String into the wrapper object. The signature of this method is:
public static <wrapperType> valueOf(String s, int radix) throws NumberFormatException
You can invoke this method only on the wrappers for integral primitives. Therefore, <wrapperType> can be only the classes Byte, Short, Integer or Long. Only these four classes have this version of valueOf method.
You can invoke this method to create a wrapper object of Integer type from a binary number as:
Integer intObj = Integer.valueOf("10100000", 2);
The valueOf() method first converts the binary number 10100000 into a int value 160 and then wraps it into an Integer object and returns the reference to that integer object.