Wrapper Classes in Java

Last Updated : 16 Jan, 2026

In Java, wrapper classes allow primitive data types to be represented as objects. This enables primitives to be used in object-oriented features such as collections, generics, and APIs that require objects.

  • Each wrapper class encapsulates a corresponding primitive value inside an object (e.g., Integer for int, Double for double).
  • Java provides wrapper classes for all eight primitive data types to support object-based operations.

Example: Converting Primitive to Wrapper (Autoboxing)

Java
class Geeks {
    public static void main(String[] args) {

        int b = 357;
        // Autoboxing: primitive int -> Integer object
        Integer a = b;

        System.out.println("The primitive int b is: " + b);
        System.out.println("The Integer object a is: " + a);
    }
}

Output
The primitive int b is: 357
The Integer object a is: 357

Explanation: Here, the primitive int value is automatically converted into an Integer object by Java. This automatic conversion is called autoboxing.

Why Wrapper Classes Are Needed

Wrapper classes are required in Java for the following reasons:

  • Java collections (ArrayList, HashMap, etc.) store only objects, not primitives.
  • Wrapper objects allow primitives to be used in object-oriented features like methods, synchronization, and serialization.
  • Objects support null values, while primitives do not.
  • Wrapper classes provide utility methods such as compareTo(), equals(), and toString().

Autoboxing and Unboxing

1. Autoboxing

The automatic conversion of primitive types to the object of their corresponding wrapper classes is known as autoboxing. For example: conversion of int to Integer, long to Long, double to Double, etc. 

Java program to demonstrate the automatic conversion of primitive to wrapper class (Autoboxing).

Java
import java.util.ArrayList;

class Geeks {
    public static void main(String[] args) {
        char ch = 'a';

        // Autoboxing: char -> Character
        Character c = ch;

        ArrayList<Integer> list = new ArrayList<>();
        // Autoboxing: int -> Integer
        list.add(25);
        System.out.println(list.get(0));
    }
}

Output
25

2. Unboxing

Unboxing is the automatic conversion of a wrapper class object back into its corresponding primitive type.

Java
import java.util.ArrayList;

class Geeks {
    public static void main(String[] args) {

        Character ch = 'a';
        // Unboxing: Character -> char
        char c = ch;

        ArrayList<Integer> list = new ArrayList<>();
        list.add(24);
        // Unboxing: Integer -> int
        int num = list.get(0);

        System.out.println(num);
    }
}

Output
24

Java Wrapper Classes Example

Java
class Geeks {
    public static void main(String[] args) {

        byte b = 1;
        Byte byteObj = Byte.valueOf(b);

        int i = 10;
        Integer intObj = Integer.valueOf(i);

        float f = 18.6f;
        Float floatObj = Float.valueOf(f);

        double d = 250.5;
        Double doubleObj = Double.valueOf(d);

        char c = 'a';
        Character charObj = c; // autoboxing

        System.out.println("Wrapper Objects:");
        System.out.println(byteObj);
        System.out.println(intObj);
        System.out.println(floatObj);
        System.out.println(doubleObj);
        System.out.println(charObj);

        // Unboxing
        byte bv = byteObj;
        int iv = intObj;
        float fv = floatObj;
        double dv = doubleObj;
        char cv = charObj;

        System.out.println("\nUnwrapped values:");
        System.out.println(bv);
        System.out.println(iv);
        System.out.println(fv);
        System.out.println(dv);
        System.out.println(cv);
    }
}

Output:

WrapperClassOutput
Output

Primitive Data Types and their Corresponding Wrapper Classes

Primitive Data TypeWrapper Class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

Common Methods of Wrapper Classes

MethodDescriptionExample
parseXxx(String s)Converts a String into its corresponding primitive typeint a = Integer.parseInt("100");
valueOf(String s)Converts a String into a wrapper objectInteger a = Integer.valueOf("100");
valueOf(primitive)Converts a primitive value into a wrapper objectInteger a = Integer.valueOf(10);
xxxValue()Converts a wrapper object into its primitive typeint a = obj.intValue();
toString()Converts wrapper object into a StringString s = Integer.toString(10);
compareTo(Xxx obj)Compares two wrapper objectsa.compareTo(b);
equals(Object obj)Compares values, not referencesa.equals(b);
hashCode()Returns hash code of the objectobj.hashCode();
min(x, y)Returns minimum of two valuesInteger.min(10, 20);
max(x, y)Returns maximum of two valuesInteger.max(10, 20);
sum(x, y)Returns sum of two valuesInteger.sum(10, 20);
compare(x, y)Compares two primitive valuesInteger.compare(10, 20);
isNaN()Checks if value is Not a Number (Float/Double only)Double.isNaN(val);
isInfinite()Checks infinite value (Float/Double only)Double.isInfinite(val);
decode(String s)Decodes decimal, hex, or octal stringInteger.decode("0xA");
parseBoolean(String s)Converts string to booleanBoolean.parseBoolean("true");

Suggested Quiz

0 Questions

Quiz Completed Successfully

Your Score : 0/0

Accuracy : 0%

Comment