Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📘 Lesson  ·  Lesson 35

Wrapper Classes in Java

In this lesson: Integer, Double, Character wrapper classes in Java — autoboxing, unboxing, parse methods — with output. In Hindi & English.

What are wrapper classes

Wrapper classes turn a primitive type into an object. Each primitive has a matching class: int → Integer, double → Double, and so on. This is needed because some parts of Java (like collections) work only with objects, not primitives.

💡 Wrapper = a primitive wrapped as an object, so it can be used where objects are required.

The wrapper classes

PrimitiveWrapper
intInteger
doubleDouble
charCharacter
booleanBoolean
longLong

Each of the eight primitives has a wrapper class. Note that most just capitalise the name, but int becomes Integer and char becomes Character.

Autoboxing and unboxing

Integer boxed = 5;     // autoboxing: int -> Integer
int unboxed = boxed;   // unboxing: Integer -> int
System.out.println(boxed + " " + unboxed);
5 5

Java converts automatically: autoboxing wraps an int into an Integer, and unboxing takes the int back out. You usually do not convert by hand — Java handles it.

Why we need them

import java.util.ArrayList;

ArrayList<Integer> list = new ArrayList<>();
list.add(10);       // autoboxed to Integer
list.add(20);
System.out.println(list.get(0));
10

Collections store objects, so you write ArrayList<Integer>, not ArrayList<int>. Wrapper classes let numbers live in these object-based collections, with autoboxing doing the conversion.

Parsing strings to numbers

int n = Integer.parseInt("123");
double d = Double.parseDouble("9.5");
System.out.println(n + d);
132.5

Wrapper classes provide parseInt and parseDouble to turn text into numbers — essential when reading numeric input as strings.

Useful wrapper methods

System.out.println(Integer.MAX_VALUE);     // biggest int
System.out.println(Integer.toBinaryString(10)); // 1010
System.out.println(Character.isDigit('5'));     // true
2147483647
1010
true

Wrapper classes carry handy constants and methods, like Integer.MAX_VALUE, base conversions, and character checks — things a plain primitive cannot offer.

Common mistakes

  • Using a primitive in a collection. Use the wrapper: ArrayList<Integer>, not <int>.
  • Null unboxing. Unboxing a null Integer to int throws a NullPointerException.
  • Comparing wrappers with ==. Use equals() for value comparison of Integer objects.
  • Wrong wrapper name. It is Integer and Character, not Int or Char.

Summary

  • Wrapper classes turn primitives into objects (int → Integer).
  • Autoboxing and unboxing convert between them automatically.
  • Collections need wrappers because they store objects.
  • Use parseInt/parseDouble to convert strings to numbers.
  • Wrappers add useful constants and methods primitives lack.

Next, store them in an ArrayList, or convert types with type casting.

Frequently Asked Questions

What is a wrapper class in Java?
A wrapper class turns a primitive type into an object. For example Integer wraps int, and Double wraps double. This lets primitives be used where objects are required, like in collections.
What is autoboxing and unboxing?
Autoboxing is Java automatically converting a primitive to its wrapper object (int to Integer). Unboxing is the reverse (Integer to int). Java does both for you, so you rarely convert manually.
Why do collections need wrapper classes?
Collections like ArrayList store objects, not primitives. So you use ArrayList<Integer>, not ArrayList<int>. Wrapper classes let primitive values live inside these object-based collections.
How do I convert a String to an int?
Use Integer.parseInt("123"), which returns the int 123. Similarly Double.parseDouble("9.5") gives a double. These wrapper methods are the standard way to turn text into numbers.
What is the difference between int and Integer?
int is a primitive that stores a value directly and cannot be null. Integer is an object that wraps an int, can be null, and has useful methods, but uses a little more memory.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicMultithreading Your progress: 35 / 61 (57%) · Lesson 36 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free