java.lang package provides a wrapper class for each of the primitive data types. Since Java is an object-oriented language, these wrapper classes help us in treating our primitives much like any other Java object.
All wrapper classes are final. Also, the objects instantiated of these wrapper classes are immutable i.e. the value stored within an object can’t be changed.
In this quick tutorial, we’ll look at the hierarchy of the available wrapper classes and how we can use them.
All wrapper classes in Java are available in java.lang package and have the following hierarchical relationship :
Clearly, java.lang.Long and java.lang.Integer is nowhere directly related, apart from the fact that they both are the classes inherited from java.lang.Number. So, it will be a bad idea when trying out:
Integer a = new Integer(10); Long b = a; // compile-time error (incompatible types)
java.lang.Number is an abstract class which is a parent of all numeric wrapper classes.
WrapperClass(type primitiveValue) WrapperClass(String strValue)
The first one accepts a value of its primitive type and the other accepts its value in its String equivalent. Some example usages look like:
Byte a = new Byte(10); Integer b = new Integer("12"); Boolean c = new Boolean("123");// assigns false
The compiler will throw a NumberFormatException in case the strValue is not compatible with its type(except for Boolean type where anything other than true is considered false):
Integer score = new Integer("10aa"); //throws NumberFormatException
java.lang.Character class has only one public constructor which accepts a character primitive.
Integer a = 10; //auto-boxing
The compiler here itself converts the primitive type to an object of its type.
static WrapperClass valueOf(type primitiveVal) static WrapperClass valueOf(String strValue)
Character class only has one valueOf() method definition accepting a char primitive value. Some example usages of this method:
Integer a = Integer.valueOf("10"); Long b = Long.valueOf("12"); Double c = Double.valueOf("12.3"); Boolean d = Boolean.valueOf("TRue"); //ignores casing Integer e = Integer.valueOf(10); Boolean f = Boolean.valueOf(true);
type typeValue()
where type refers to a primitive type:
boolean booleanValue() //in Boolean class char charValue() // in Character class byte byteValue() short shortValue() int intValue() long longValue() float floatValue() double doubleValue()
Also, all the six numeric variants of this method are available in all six numeric wrapper classes as they are defined in java.lang.Number.
Integer a = new Integer("2"); int b = a; //unboxing
Every wrapper class, except Character, provides a static method of the form:
static type parseType(String str)
This method returns a primitive value. Usage looks like:
boolean a = Boolean.parseBoolean("10"); // assigns a = false boolean b = Boolean.parseBoolean("TruE"); //assigns b = true double i = Double.parseDouble("123.3"); //assigns i = 123.3
Wrapper classes provide a static toString() method which converts a primitive type to its corresponding String object:
static toString(type value)
Some examples:
String a = Character.toString('a'); String b = Byte.toString(10); String c = Double.toString(12.22);
In this quick tutorial, we have learned about Wrapper classes in Java, how we can create a wrapper object and how can it be converted to its primitive form. We also looked at some methods exposed by these classes which can help us convert a String to a primitive type and vice-versa.