Wrapper Classes in Java (original) (raw)

Last Updated : 6 Apr, 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.

**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:

**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));
}

}

`

**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);
}

}

`

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 & Wrapper Classes

Primitive Data Type Wrapper Class
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean

Common Methods of Wrapper Classes

Method Description Example
parseXxx(String s) Converts a String into its corresponding primitive type int a = Integer.parseInt("100");
valueOf(String s) Converts a String into a wrapper object Integer a = Integer.valueOf("100");
valueOf(primitive) Converts a primitive value into a wrapper object Integer a = Integer.valueOf(10);
xxxValue() Converts a wrapper object into its primitive type int a = obj.intValue();
toString() Converts wrapper object into a String String s = Integer.toString(10);
compareTo(Xxx obj) Compares two wrapper objects a.compareTo(b);
equals(Object obj) Compares values, not references a.equals(b);
hashCode() Returns hash code of the object obj.hashCode();
min(x, y) Returns minimum of two values Integer.min(10, 20);
max(x, y) Returns maximum of two values Integer.max(10, 20);
sum(x, y) Returns sum of two values Integer.sum(10, 20);
compare(x, y) Compares two primitive values Integer.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 string Integer.decode("0xA");
parseBoolean(String s) Converts string to boolean Boolean.parseBoolean("true");