Table of content

Wrapper classes in Java

Wrapper classes in java are nothing but the classes which are used to convert the primitive data types into Objects.

As their name suggests, they are used to wrap the primitive data types into an Object type. The primitive data types are not objects, so they do not belong to any class.

Collections only store and support objects, so it is required to convert the primitive type to object first, which we can do by using wrapper classes.

All the wrapper classes are subclasses to Object Class, All the number-related classes are sub-class to Number class. The Number class is an abstract class in Java.

Note: There are four other subclasses of Numbers that are not discussed here. BigDecimal and BigInteger are used for high-precision calculations. AtomicInteger and AtomicLong are used for multi-threaded applications.

Boolean class (for boolean) :

Boolean wrapper class converts the boolean primitive data into Boolean Object.

Number class (for number) :

All the number related classes are inherited from the Number class.

Character class (for character) :

Character wrapper class converts the character primitive values into a character object.

Byte class (for byte) :

Byte wrapper class converts the byte value into Byte Object

Integer class (for int) :

Integer class converts the primitive value into Integer values; remaining wrapper classes also converts primitive values into respective Object value.

We will be discussing the methods present in the Integer class. Almost all the remaining classes have similar methods only.

Integer Wrapper class

The Integer class has two constructors: 1. Integer(int i) : accepts the integer parameter and creates the object for the int.

Integer intObj1 = new Integer(10);

2. Integer(String s) : accepts String parameter, converts the accepted string parameter into int and creates Integer object for the int.

Integer intObj2 = new Integer("89");

Instead of using the above two forms, it is always better to use the below way of creating the object and which takes less memory.

Integer i  = Integer.valueOf("100");

Below are few non-static methods present in the Integer class

byteValue() :

Returns the value of this Integer as a byte.

compareTo(Integer anotherInteger) :

Compares two Integer objects numerically.

intValue() :

Returns the value of this Integer as an int.

toString() :

Returns a String object representing this Integer's value. Below are few static methods present in the Integer class.

parseInt(String s) :

Parses the string argument as a signed decimal integer.

valueOf(String s) :

Returns an Integer object holding the value of the specified String.

toString(int i) :

Returns a String object representing the specified integer.

Reasons to use Number class than primitive values :
There are three reasons that you might use a Number object rather than a primitive

  • As an argument of a method that expects an object (often used when manipulating collections of numbers).
  • To use constants defined by the class, such as MIN_VALUE and MAX_VALUE, that provide the upper and lower bounds of the data type.
  • To use class methods for converting values to and from other primitive types, for converting to and from strings, and for converting between number systems (decimal, octal, hexadecimal, binary).

Features of the Java wrapper Classes

  • Wrapper classes convert numeric strings into numeric values.
  • The way to store primitive data in an object.
  • The valueOf() method is available in all wrapper classes except Character
  • All wrapper classes have the typeValue() method. This method returns the value of the object as its primitive type.

Reasons for using Wrapper classes :

  • Allows null values
  • It can be used in collections such as List, Map, etc.
  • It can be used in methods that accept arguments of the Object type.
  • Makes available all the functions that Object class has such as clone(), equals(), hashCode(), toString() etc.

Boxing :

The process of converting the primitive data type into an Object using the Wrapper class is called Boxing. For example, converting int to Integer.

Auto-Boxing :

Auto-boxing is nothing but converting primitive data into Objects automatically; this is the reason we are able to store our primitive values in the Collections.

The Java compiler can directly convert the primitive types into corresponding objects. This process is known as auto-boxing. Auto-boxing happens-

  • when a method is expecting a wrapper class object but the value that is passed as a parameter is a primitive type. Example-
    public static void autoboxMethod(Integer num){
   public static void main(String[] args) {
       //passed int (primitive type) would be converted to Integer object at Runtime


  • When at some point of time, you are assigning a primitive type value to an object of its wrapper class. The compiler does the autoboxing at runtime.
public static void main(String args[]){  
    int a=14;  
        Integer obja=new Integer(a);  
        Integer b=5;  


  • When dealing with collection framework classes. In the below example, here ArrayList class is expecting an Integer wrapper class object but we are providing int primitive. So autoboxing happens.
public static void main(String[] args) {
      ArrayList<Integer> list = new ArrayList<>();
      System.out.println("ArrayList: " + list);


ArrayList: [1, 2]

Un-Boxing :

Unboxing is the process of converting the primitive values which are wrapped as Objects into primitive values.

Unboxing happens when-

  • The method is expecting Integer object (parameter) but we have given int. In such a case automatic conversion(unboxing) happens that converts Integer to int.
public static void unboxMethod(int num){
   public static void main(String[] args) {
    	Integer num = new Integer(14); 	
       //passed Integer wrapper class object 
       //would be converted to int primitive type at Runtime    


  • During assignments like-
 public static void main(String args[]){  
    Integer i=new Integer(14);  
    //unboxing int type to Integer object
        int a=i;         


  • When dealing with collection framework classes-

In the below example, the object is automatically converted into the primitive type int and assigned to the variable a-

public static void main(String[] args) {

  ArrayList < Integer > list = new ArrayList < > ();
  System.out.println("ArrayList is " + list);
  // unboxing
 //here the get() method returns the object at the index 0
  int a = list.get(0);
  System.out.println("Value at index 0 is " + a);


ArrayList is [1, 2]
Value at index 0 is 1

Auto-Unboxing :

Auto-Unboxing is nothing but converting Objects into primitive values automatically. With respect to java boxing and Unboxing occurs automatically, so JVM uses boxing and unboxing auto-boxing and auto-unboxing to store the primitive values in the list and to retrieve from the list.

import java.util.ArrayList;
import java.util.List;

public class IntegerExample {
	public static void main(String[] args) {
		// below is the process of converting int -> Integer
		// also known as boxing
		int i = 10; // primitive value
		Integer intObj = new Integer(10);

		// below is example for auto-boxing
		List al = new ArrayList();
		// syntax of adding a value to list
		// al.add(Object e);
		// but in place of Object we are able to store the int
		// int is not stored as int but as Integer
		//and then converted into Object class object

		// unboxing

		// on below line int stored as object is Un-boxed and became as int
		// this is called as auto-unboxing


About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • Prashant Jain
    Nice & knowledgable article!!!
    Have you worked on browsermob proxy?
    I have an query’s in browsermob proxy like how we can wait till the network layer of browser ends??
    • karthiQ [ admin]
      Hi Prashant Jain,
      I have not worked on browsermob proxy but i would try to add an tutorial on it very soon.