Wrapper class in Java

Filed Under: Java

Wrapper class in java are the Object representation of eight primitive types in java. All the wrapper classes in java are immutable and final.

Java 5 autoboxing and unboxing allows easy conversion between primitive types and their corresponding wrapper classes in java programs.

Wrapper Class in Java

wrapper class in java, wrapper classes in java

Below table shows the primitive types and their wrapper class in java.

Primitive typeWrapper classConstructor Arguments
byteBytebyte or String
shortShortshort or String
intIntegerint or String
longLonglong or String
floatFloatfloat, double or String
doubleDoubledouble or String
booleanBooleanboolean or String

Why do we need wrapper classes?

I think it was a smart decision to keep primitive types and Wrapper classes separate to keep things simple. We need wrapper classes when we need a type that will fit in the Object-Oriented Programming like Collection classes. We use primitive types when we want things to be simple.

Primitive types can’t be null but wrapper classes can be null.

Wrapper classes can be used to achieve polymorphism.

Here is a simple program showing different aspects of wrapper classes in java.


package com.journaldev.misc;

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

public class WrapperClasses {

    private static void doSomething(Object obj){
    public static void main(String args[]){
        int i = 10;
        char c = 'a';
        //primitives are simple to use
        int j = i+3;
        //polymorphism achieved by Wrapper classes, we can't pass primitive here
        doSomething(new Character(c));
        List<Integer> list = new ArrayList<Integer>();
        //wrapper classes can be used in Collections
        Integer in = new Integer(i);
        //autoboxing takes care of primitive to wrapper class conversion
        //wrapper classes can be null
        in = null;


  1. rahul says:

    all contents that you can provide is good and very useful when I do my coding.

  2. Manvendra says:

    that was cool information about the wrapper class basics

  3. Neeraj says:

    What does it mean “Wrapper classes can be used to achieve polymorphism” ??

    1. Aliasger says:

      As show in the example the doSomething(Object object) can take any generic parameter since the attribute is of type Object i.e Superclass of all object. This cannot be achieved with primitive data type.

      1. Ashutosh says:

        Better to say it can achieve runtime polymorphism

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors