Ugenz Academy/Tutorial

Wrapperclasses

5 min read850 words

Reading progress

Keep learning

Wrapper Classes in Java

Introduction

Wrapper classes in Java are classes that convert primitive data types into objects.

Java provides a wrapper class for each primitive data type.

For example:

int → Integer double → Double char → Character boolean → Boolean

Wrapper classes are available mainly in the java.lang package, so they do not need to be imported.

Primitive Types and Wrapper Classes

Primitive TypeWrapper Class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

Why Use Wrapper Classes?

Wrapper classes are useful because:

  • Collections require objects instead of primitive types.
  • They provide useful methods for conversion and comparison.
  • They allow primitive values to be treated as objects.
  • They support null values.
  • They are useful in Generics.

Creating Wrapper Objects

public class WrapperExample { public static void main(String[] args) { Integer number = 100; Double price = 50.5; Character grade = 'A'; Boolean status = true; System.out.println(number); System.out.println(price); System.out.println(grade); System.out.println(status); } }

Output

100 50.5 A true

Autoboxing

Autoboxing is the automatic conversion of a primitive type into its corresponding wrapper object.

Example:

int number = 100; Integer obj = number;

Java automatically converts:

int → Integer

Example

public class AutoboxingExample { public static void main(String[] args) { int number = 50; Integer obj = number; System.out.println(obj); } }

Output

50

Unboxing

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

Example:

Integer obj = 100; int number = obj;

Java automatically converts:

Integer → int

Example

public class UnboxingExample { public static void main(String[] args) { Integer obj = 100; int number = obj; System.out.println(number); } }

Output

100

Autoboxing and Unboxing Example

public class BoxingExample { public static void main(String[] args) { int a = 10; // Autoboxing Integer obj = a; // Unboxing int b = obj; System.out.println(obj); System.out.println(b); } }

Output

10 10

Wrapper Classes with Collections

Java Collections cannot directly store primitive types.

For example, this is not valid:

// Not allowed ArrayList<int> numbers;

Instead, use the wrapper class:

ArrayList<Integer> numbers = new ArrayList<>();

Example

import java.util.*; public class WrapperCollection { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<>(); numbers.add(10); numbers.add(20); numbers.add(30); System.out.println(numbers); } }

Output

[10, 20, 30]

Here, Java automatically performs autoboxing when primitive int values are added to the ArrayList<Integer>.

Useful Wrapper Class Methods

Wrapper classes provide useful methods for conversion and checking values.

Integer

int number = Integer.parseInt("100"); System.out.println(number);

Output:

100

Double

double price = Double.parseDouble("99.50"); System.out.println(price);

Output:

99.5

Boolean

boolean result = Boolean.parseBoolean("true"); System.out.println(result);

Output:

true

Common Methods

MethodPurpose
parseInt()Converts String to int
parseDouble()Converts String to double
parseFloat()Converts String to float
parseLong()Converts String to long
toString()Converts a value to String
compareTo()Compares two wrapper objects

Primitive vs Wrapper Class

Primitive TypeWrapper Class
intInteger
Stores a simple valueStores a value as an object
Cannot be nullCan be null
Used for basic operationsUsed when an object is required
Faster for simple operationsHas additional methods

Real-Life Example

Suppose a student has an age:

int age = 20;

If the age needs to be stored inside a collection:

ArrayList<Integer> ages = new ArrayList<>(); ages.add(age);

Java automatically performs:

int → Integer

This is autoboxing.

Advantages of Wrapper Classes

  1. Convert primitive values into objects.
  2. Support Java Collections and Generics.
  3. Provide useful conversion methods.
  4. Support null values.
  5. Provide methods for comparison and manipulation.
  6. Enable automatic boxing and unboxing.

Important Points

  • Wrapper classes represent primitive data types as objects.
  • Integer is the wrapper class for int.
  • Double is the wrapper class for double.
  • Character is the wrapper class for char.
  • Autoboxing converts primitive → wrapper object.
  • Unboxing converts wrapper object → primitive.
  • Wrapper classes are especially important when working with Collections and Generics.
  • Wrapper classes are mainly located in the java.lang package.

Conclusion

Wrapper classes allow Java primitive values to be used as objects. They are especially important when working with Collections, Generics, and object-based operations. Autoboxing and unboxing make the conversion between primitive types and wrapper objects automatic.

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy