Wrapper Classes


Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type.
Following table lists the primitive types and the corresponding wrapper classes:


PrimitiveWrapper
boolean  java.lang.Boolean
byte  java.lang.Byte
char  java.lang.Character
double  java.lang.Double
float  java.lang.Float
int  java.lang.Integer
long  java.lang.Long
short  java.lang.Short
void  java.lang.Void


  • one for each primitive type: Boolean, Byte, Character, Double, Float, Integer, Long, and Short
  • Byte, Double, Float, Integer and Short extend the abstract Number class
  • all are public final ie cannot be extended
  • get around limitations of primitive types
  • allow objects to be created from primitive types
    • all, except Character, have a valueOf(String s) method which is equivalent to new Type(String s)
    • all have a typeValue() method which returns the value of the object as it's primitive type. These are all abstract methods defined in Number and overridden in each class
      • public byte byteValue()
      • public short shortValue()
      • public int intValue()
      • public long longValue()
      • public float floatValue()
      • public double doubleValue()
    • all the classes override equals(), hashCode() and toString() in Object
      • equals() returns true if the values of the compared objects are the same
      • hashCode() returns the same hashcode for objects of the same type having the same value
      • toString() returns the string representation of the objects value
    • all have a public static final TYPE field which is the Class object for that primitive type
    • all have two static fields MIN_VALUE and MAX_VALUE for the minimum and maximum values that can be held by the type
  • all the classes have two constructor forms
    • a constructor that takes the primitive type and creates an object eg Character(char), Integer(int)
    • a constructor that converts a String into an object eg Integer("1"). Throws a NumberFormatException if the String cannot be converted to a number
    The wrapper classes in the Java API serve two primary purposes:
    • To provide a mechanism to “wrap” primitive values in an object so that the primitives can be included in activities reserved for objects, like as being added to Collections, or returned from a method with an object return value.
    • To provide an assortment of utility functions for primitives. Most of these functions are related to various conversions: converting primitives to and from String objects, and converting primitives and String objects to and from different bases (or radix), such as binary, octal, and hexadecimal.
    The wrapper object of a wrapper class can be created in one of two ways: by instantiating the wrapper class with the new operator or by invoking a static method on the wrapper class. We will explore this further in this article.


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email