Core Java

Java Wrapper Classes

Introduction:

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.

Wrapper Classes Hierarchy:

All wrapper classes in Java are available in java.lang package and have the following hierarchical relationship :

Java Wrapper Class HIerarchyClearly, 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.

Instantiating a Wrapper Class:

  • Constructor: All wrapper classes, except Character, expose two public constructors:
    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.

  • Autoboxing feature: Java provides an auto-boxing feature which the help of which we can write:
    Integer a = 10; //auto-boxing

    The compiler here itself converts the primitive type to an object of its type.

  • valueOf() method: Each wrapper class, except Character, defines two static methods valueOf():
    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);
    

Converting Wrapper Object to Primitive Type:

  • typeValue() method: All wrapper classes define a method with format:
    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.

  • Unboxing feature: Our compiler is intelligent enough to automatically unbox a wrapper type to its primitive equivalent:
    Integer a = new Integer("2");
    int b = a; //unboxing

Parsing a String object to Primitive Type:

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

Converting Primitive Values to String:

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);

Conclusion:

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.

Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *