Java Wrapper Classes Tutorial with Examples

This tutorial explains the concepts of Java wrapper classes with examples, shows how to convert from wrapper types to primitives and vice-a-versa, explains numeric wrapper classes and wraps up with character & boolean wrapper classes Character.

What are wrapper classes
Java Wrapper Classes are used to hold primitive data type as Objects. In java primitive data types are not objects. Wrapper classes are wrappers which take a primitive data value and in a sense wrap it an Object. We can then pass these around wherever objects can be passed.
Moreover, there are multiple data structures which do not allow primitives such as keys cannot be primitives in a HashMap, ArrayList elements cannot be primitives and so on. So, if we have primitive data values we can wrap them in an equivalent Wrapper type so that we can use them in all places where only Objects are allowed.

Primitive types and their corresponding Wrapper Types

Primitive Wrapper Type
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double

Conversion of primitives to wrapper types via Wrapper Class Constructors (Common to all primitives)

  • Constructors with primitive as parameter: Parameterized constructors of wrapper classes which take corresponding primitive value as parameter can be used to create wrapper object instances.
    For example:
    Character characterObj=new Character('a');
    Float floatObj=new Float(1.25);
    Integer intObj=new Integer(100);
    Boolean boolObj=new Boolean(true);
    and so on
  • Constructors with String equivalent of primitive as parameter: Parameterized constructors which take String values to create their wrapper equivalents.
    For example:
    Integer intObj=new Integer("200");
    Float floatObj=new Float("1.50");
    Boolean boolObj=new Boolean("false");
    and so on

Utility Methods for conversion from wrapper type to primitives and vice-a-versa(Common to all primitives)

  • Converting Wrapper Objects to Strings: All wrapper classes override the toString() method to produce the equivalent String value.
    For example:
    String intStr=new Integer(100).toString();
    intStr gets the String value “100”.
    Note – The same method toString() can be used to convert primitive values to String values by passing a primitive value to the method instead of a String value.
  • Converting Wrapper Objects to primitive values: Each wrapper type has a method typeValue(). This method when applied to the Wrapper Type produces the equivalent primitive value.
    For Example:
    int intVal=new Integer(120).intValue();
    intVal gets the primitive int value 120.
  • Converting Strings to Wrapper Objects:This is accomplished by using the valueOf(String str) method which is there in every wrapper class. Only character type does not have this method.
    For Example:
    Boolean booleanObj=String.valueOf("true");
    Float floatObj=String.valueOf("0.25");
    and so on

Equals & Hashcode in wrapper types(Common to all primitives)

  • Equals: All wrapper classes override the equals() method of Object class. Format of the equals method which is as defined in the Object class is – boolean equals(Object obj2)
  • Hashcode:All wrapper classes override the hashcode() method of Object class. The hashcode value is calculated based on the primitive value in the wrapper class instance

Numeric Types
There are six numeric types – Byte, Short, Int, Long, Float and Double out of the eight wrapper types. There are certain important features which are specific to, and found in, all numeric wrapper classes –

  • Every numeric type has constants by the name MIN_VALUE & MAX_VALUE which contain the minimum value and the maximum value possible for that wrapper type(which is same as the minimum & maximum values of corresponding primitive types).
  • All numeric wrapper classes have a static method parseType(String s). This method takes a String value and returns a wrapper of the type Type passed to it.For example: int value=Integer.parseInt("100");//assigns a value 100 to int
    Important Note – The parseType(String s) method throws a NumberFormatException if the String passed to it cannot be parsed into an equivalent Type wrapper numeric value.

Character & Boolean wrapper classes
Character and Boolean are some of the lesser used wrapper classes.
Boolean wrapper class defines two constants TRUE & FALSE which are equivalent to Boolean objects with true & false value respectively.
Character wrapper class has two constants MIN_VALUE & MAX_VALUEwhich contain the minimum & maximum values possible for a Character object. In addition, Character wrapper types also define some basic character utility methods like toUpperCase() & toLowerCase() (self explanatory), and various methods to check whether the given character is uppercase/lowercase/titlecase/digit and to convert a given character to uppercase/lowercase/titlecase.

This concludes the tutorial of wrapper classes where we covered why wrapper classes exist, what are the eight wrapper classes, common utility methods,features specific to numeric types and finally ending with details of Character & Boolean wrapper classes.

 

Digiprove sealCopyright © 2014-2016 JavaBrahman.com, all rights reserved.