Login Register
×

What a Wrapper Classes in Java?

What is a Wrapper Classes?

In Java, Wrapper Classes are special classes that convert primitive data types (such as int, char, and Boolean) into objects.

They are called wrapper classes because they literally “wrap” the primitive value inside an object, giving it extra abilities that a normal primitive variable doesn’t have.

Java provides a separate wrapper class for each primitive type, and all these classes are part of the java.lang package, which is automatically imported in every Java program.

Why Do We Need Wrapper Classes?

In Java, primitives are not objects; they are simple values that live in memory for fast processing.

However, there are many cases in real-world programming where you must use objects instead of primitives. For example:

  • Working with Collections like ArrayList, HashMap, or HashSet, these store only objects, not primitive data types.
  • Data conversion and manipulation using methods available only in objects (e.g., converting a string to a number).
  • Primitives can’t store null values, but wrapper objects can do this.
  • Autoboxing and Unboxing: Java automatically converts between primitives and their wrappers when needed.

Example: Primitive vs Wrapper Class

public class WrapperExample {
    public static void main(String[] args) {
        // Primitive data type
        int num = 50;

        // Wrapper class object (Boxing)
        Integer wrappedNum = Integer.valueOf(num);

        // Using wrapper class methods
        System.out.println("Wrapped Value: " + wrappedNum);
        System.out.println("Square of value: " + (wrappedNum * wrappedNum));

        // Unboxing (converting back to primitive)
        int unboxedNum = wrappedNum.intValue();
        System.out.println("Unboxed Value: " + unboxedNum);
    }
}

Output:

Wrapped Value: 50
Square of value: 2500
Unboxed Value: 50

List of Wrapper Classes in Java

Primitive TypeWrapper Class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

Examples of Wrapper Class Usage

Wrapper classes are extremely useful in real-world Java applications. They make it possible to use primitive values like int, char, or Boolean in object-based operations such as collections, type conversions, and utility method calls.

1. Using Wrapper Classes with Collections

Normally, Java collections (like ArrayList, HashMap, etc.) do not store primitive data types; they only store objects.

The Wrapper class “wrap” primitive values into object form automatically (this process is called autoboxing).

For example:

import java.util.ArrayList;

public class WrapperWithCollection {
public static void main(String[] args) {
// Create an ArrayList to store Integer objects
ArrayList<Integer> numbers = new ArrayList<>();

// Add primitive values (autoboxing happens automatically)
numbers.add(5); // int 5 → Integer(5)
numbers.add(15); // int 15 → Integer(15)
numbers.add(25);

// Retrieve and print elements (unboxing occurs automatically)
int first = numbers.get(0); // Integer → int
System.out.println("First number: " + first);

// Loop through the list
for (Integer num : numbers) {
System.out.println("Number: " + num);
}
}
}

Output:

First number: 5
Number: 5
Number: 15
Number: 25
  • In this code, Java automatically converts the primitive int values into Integer objects when added to the list (autoboxing), and converts them back when retrieved (unboxing).

2. Conversion Between Primitives and Strings

Wrapper classes also make it easy to convert between strings and primitive values. This is useful when reading input from users, files, or APIs, since all input comes as text (String).

public class ConversionDemo {
public static void main(String[] args) {
// Convert primitive to String
int age = 25;
String ageStr = String.valueOf(age);
System.out.println("String form of age: " + ageStr);

// Convert String to primitive
String numText = "500";
int number = Integer.parseInt(numText);
System.out.println("Converted integer value: " + number);

// Another example with Double
String priceText = "99.99";
double price = Double.parseDouble(priceText);
System.out.println("Converted double value: " + price);
}
}

Output:

String form of age: 25
Converted integer value: 500
Converted double value: 99.99

3. Using Utility Methods from Wrapper Classes

Each wrapper class comes with built-in utility methods that make programming easier, such as checking character types, comparing values, and getting limits of a data type.

public class WrapperUtilityDemo {
public static void main(String[] args) {
// Using constants from wrapper classes
System.out.println("Max value of Integer: " + Integer.MAX_VALUE);
System.out.println("Min value of Integer: " + Integer.MIN_VALUE);

// Checking if a character is a digit or a letter
char ch1 = '9';
char ch2 = 'A';

boolean isDigit = Character.isDigit(ch1);
boolean isLetter = Character.isLetter(ch2);

System.out.println("'" + ch1 + "' is a digit? " + isDigit);
System.out.println("'" + ch2 + "' is a letter? " + isLetter);

// Comparing two numbers using wrapper methods
int result = Integer.compare(30, 20);
if (result > 0) {
System.out.println("30 is greater than 20");
}
}
}

Output:

Max value of Integer: 2147483647
Min value of Integer: -2147483648
'9' is a digit? true
'A' is a letter? true
30 is greater than 20

In this code:

  • MAX_VALUE and MIN_VALUE show the range of primitive types.
  • Character.isDigit() and Character.isLetter() check the type of a character.
  • Integer.compare() compares two integer values easily.

Autoboxing and Unboxing in Java

In Java, Autoboxing and Unboxing are automatic conversion processes that make it easy to switch between primitive data types (like int, double, char) and their Wrapper Classes (Integer, Double, Character, etc.).

What is Autoboxing?

Autoboxing is the process by which Java automatically converts a primitive value into its corresponding wrapper class object.

Example:

public class AutoboxingExample {
public static void main(String[] args) {
// Primitive value
int number = 5;

// Autoboxing: converting primitive to object automatically
Integer boxedNumber = number;

// Printing both
System.out.println("Primitive int: " + number);
System.out.println("Wrapped Integer object: " + boxedNumber);

// Using autoboxing in collections
java.util.ArrayList<Integer> list = new java.util.ArrayList<>();
list.add(10); // Java automatically boxes int to Integer
list.add(20);
list.add(30);

System.out.println("ArrayList with boxed values: " + list);
}
}

Output:

Primitive int: 5
Wrapped Integer object: 5
ArrayList with boxed values: [10, 20, 30]

In this code:

  • When you add a primitive like 10 or 20 to an ArrayList<Integer>, Java automatically converts it into an Integer object; this is Autoboxing in action.

What is Unboxing in Java?

Unboxing is the reverse of autoboxing. It’s when Java automatically converts a wrapper class object back to its primitive value.

Example of Unboxing:

public class UnboxingExample {
public static void main(String[] args) {
// Wrapper class object
Integer wrappedValue = 25;

// Unboxing: converting object to primitive automatically
int number = wrappedValue;

System.out.println("Wrapped Integer object: " + wrappedValue);
System.out.println("Primitive int after unboxing: " + number);

// Example inside a loop
java.util.ArrayList<Integer> numbers = new java.util.ArrayList<>();
numbers.add(100);
numbers.add(200);
numbers.add(300);

int sum = 0;
for (Integer num : numbers) {
sum += num; // Unboxing happens here
}

System.out.println("Sum of numbers: " + sum);
}
}

Output:

Wrapped Integer object: 25
Primitive int after unboxing: 25
Sum of numbers: 600
  • When you perform arithmetic (sum += num), Java automatically extracts the primitive value from the wrapper object, that’s Unboxing.

Important Wrapper Class Methods

1) Value Conversion Methods: These methods convert wrapper objects back to their primitive values. Each wrapper class has methods like intValue(), doubleValue(), floatValue(), etc.

For example:

  • Integer → intValue()
  • Double → doubleValue()
  • Float → floatValue()
  • Long → longValue()

Example: Converting Wrapper Object to Primitive

public class ValueConversionExample {
public static void main(String[] args) {
// Creating wrapper objects
Integer intObj = 100;
Double doubleObj = 25.75;

// Converting to primitive types
int num = intObj.intValue(); // Integer → int
double price = doubleObj.doubleValue(); // Double → double

System.out.println("Integer Object to int: " + num);
System.out.println("Double Object to double: " + price);
}
}

Output:

Integer Object to int: 100
Double Object to double: 25.75
  • These methods extract the real numeric value from their object forms.

2) Parsing Strings into Primitive Values:
Sometimes we receive numeric data in String form, for example, from user input or files. Wrapper classes allow you to convert (parse) those strings directly into primitive numbers.

Example: Parsing Strings into Numbers

public class ParsingExample {
public static void main(String[] args) {
// Converting strings to primitive values
int age = Integer.parseInt("25");
double salary = Double.parseDouble("56000.75");
boolean active = Boolean.parseBoolean("true");

System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
System.out.println("Active: " + active);
}
}

Output:

Age: 25
Salary: 56000.75
Active: true
  • Here, Methods like parseInt(), parseDouble(), and parseBoolean() read text-based values and convert them into real primitive types so you can use them in calculations or logic.

3) Static Utility Fields and Methods: Wrapper classes also include helpful constants and utility methods that make programming easier.

Example: Using Utility Methods

public class UtilityMethodsExample {
    public static void main(String[] args) {
        // Finding range of Integer
        System.out.println("Max Integer value: " + Integer.MAX_VALUE);
        System.out.println("Min Integer value: " + Integer.MIN_VALUE);

        // Checking characters
        System.out.println("Is 'A' a letter? " + Character.isLetter('A'));
        System.out.println("Is '9' a digit? " + Character.isDigit('9'));

        // Comparing two numbers
        int result = Integer.compare(10, 20);
        System.out.println("Comparing 10 and 20 gives: " + result);
    }
}

Output:

Max Integer value: 2147483647
Min Integer value: -2147483648
Is 'A' a letter? true
Is '9' a digit? true
Comparing 10 and 20 gives: -1

Also Learn Important Topics of Java