GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Wrapper Classes in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Wrapper Classes in Java

Last Updated : 28 Sep, 2026

Wrapper classes in Java provide object representations of primitive data types. They are useful when working with Java features that require objects, such as collections and generics.

  • 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.

Need Of Wrapper Classes

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
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 & 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");
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.