Wrapper Classes

As a Java Programmer, we know that all Java variables are by default mutable(changeable). To make them immutable(not changeable), the final keywordis required. In addition, we cannot put an int(or other primitive values) in a collection. As only references can be held by a collection, weare required to convert primitive values to objects using the wrapper classes that are defined in the java.util package.
The java.utiil package not only contains the wrapper classes, but also contains the String, StringBuffer, and Stringbuilder classes to handle text strings. The wrapper classes such as Byte, Character,Integer and Float are immutable. Strings are immutable but StringBuffer and StringBuilders are mutable.

We discuss in this topic the wrapper classes that were introduced to convert primitive values to objects. The java.lang package provides a wrapper class for each primitive data type. all wrapper classes are final.

The word primitive refers to a fundamental component that is used to create other,larger parts.In Java, there are eight primitive data types. These are byte,short,int,long,float,double,boolean, and char.These primitive data types are primitive in true nature.They are not objects according to the compiler,so we cannot treat them as objects.

In OOPS,everything must be an object.Therefore, Java includeswrapper classes which essentially convert primitive data types into Java objects.

All wrapper classes belong to the java.lang package, and this package is an indispensable part of java programming. At compile time, this java.lang package is automaticallyimported into every source file. This package contains the Object class that is the parent of all classes. Boolean,Character and Number are child classes of the Object class which are also wrapper classes. The Number class, is the parent for wrapper classes( Byte, Short, Integer, Long, Float, Double) to handle primitive valuesas objects.

The Object class hierarchy

When we create an object of a wrapper class, it contains a field. In this field, We can store a primitive dtaa type. It means we can wrap or contain a primitive value into a wrapper class object.for example, if we create an object of Integer wrapper class, it contains a field int and it is possible to store an integer suach as 34 or 89.Therfore, Integer is a wrapper class of int data type.


Primitive Data Types Wrapper Classes Constructor arguments taken by wrapper Classes
boolean Boolean boolean or String
byte Byte byte or String
char Character Char
double Double double or String
float Float float, double or String
int Integer int or String
long Long long or String
short Short short or String

The void class is considered as a wrapper class; it does not wrap any primitive value and is not instantiable, as it has no public constructors. It is just a place holder to hold a reference to the class object representing the Java keyword void.

Creating the Wrapper Objects:

All primitive wrapper classes in Java are immutable. Once a value is given to the objects of wrapper classes, called wrapper objects, the value cannot be changed. An ex of a wrapper object is given the following

double d = 5.0;

Double DB = new Double(d);

the object DB is a wrapper object.Wrapper objects are created with the help of constructors of wrapper classes.

Constructors of wrapper Classes:

There are 2 types of constructors for all wrapper classes, except Character.  One of the constructorstakes a primitive value as its argument, while the other constructor takes a string representation of the value as its argument. Some  examples of the constructors of wrapper classes.

Double d = new Double(56.8);
Double d = new Double("56.8");

Integer n = new Integer(56);
Integer n = newInteger("56");

Character ch = new Character('V');

Boolean b = new Boolean(true);
Boolean b = new Boolean("true");

Methods of Wrapper Classes

The important  methods of the wrapper classes are:

  • valueOf()
  • xxxValue(), where xxx can be any primitive type
  • parseXxx(), where Xxxcan be any wrapper class
  • toString()
  • toXxxString(), where Xxx can be Hex, Octal, or Bin
  • equals()

From Java 5 onwards

autoboxing and unboxing feature converts primitive into object and object into primitive automatically. The automatic conversion of primitive into object is known as autoboxing and vice-versa unboxing.

Autoboxing supports automatic conversion pf primitive types(such as int,float,double and so on) to their equivalents(Integer,Float,Double, and so on).

  1. public class AutoboxingDemo{
  2. public static void main(String args[]){
  3. //Converting int into Integer
  4. int a=20;
  5. Integer i=Integer.valueOf(a);//converting int into Integer
  6. Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally
  7. System.out.println(a+” “+i+” “+j);
  8. }}

Output: 20 20 20


wrapper types are automatically converted into primitive equivalents.

  1. public class UnboxingDemo{
  2. public static void main(String args[]){
  3. //Converting Integer to int  
  4. Integer a=new Integer(3);
  5. int i=a.intValue();//converting Integer to int
  6. int j=a;//unboxing, now compiler will write a.intValue() internally  
  7. System.out.println(a+” “+i+” “+j);
  8. }}

output: 3 3 3

One thought on “Wrapper Classes

Leave a Reply

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