Wrapper Classes in Java

Yuvaraj
3 min readJul 26, 2024

--

What are Wrapper Classes?

Imagine you have a candy. It’s sweet, tasty, but it’s just a candy. Now, imagine wrapping this candy in a beautiful paper. This wrapper adds value to the candy. It protects it, makes it look presentable, and even allows you to add decorations.

Similarly, in Java, primitive data types are like candies — they are basic data types like int, double, char, etc. They are efficient but lack certain object-oriented features.

Wrapper classes are like the wrappers for these primitive candies. They convert primitive data types into objects, providing additional functionalities and allowing them to be used in object-oriented contexts.

Why Use Wrapper Classes?

  • Object-oriented programming: Many Java features require objects. Wrapper classes enable using primitive values in object-oriented scenarios.
  • Collections: Collections like ArrayList, HashMap, etc., can only hold objects. Wrapper classes allow you to store primitive values in these collections.
  • Method parameters: Some methods expect objects as parameters. Wrapper classes can be used to pass primitive values to these methods.
  • Utility methods: Wrapper classes provide useful methods for parsing, converting, and manipulating primitive values.

Example of Wrapper Classes

Wrapper Class

Wrapper classes are the classes that allow primitive types to be accessed as objects. It wraps the primitive data type into an object of that class.

Autoboxing and Unboxing

To make working with wrapper classes easier, Java introduced autoboxing and unboxing.

  • Autoboxing: Automatically converts a primitive value to its corresponding wrapper object.
int x = 10;
Integer y = x; // Autoboxing: int to Integer
  • Unboxing: Automatically converts a wrapper object to its corresponding primitive value.
Integer x = 10;
int y = x; // Unboxing: Integer to int

Example of Using Wrapper Classes

import java.util.ArrayList;

public class WrapperClassExample {
public static void main(String[] args) {
// Using Integer wrapper class instead of int primitive type
Integer num = 100;

// Converting Integer to int (unboxing)
int primitiveNum = num;

// Autoboxing: automatically converting int to Integer
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(primitiveNum); // Automatically converts int to Integer

// Using utility methods provided by wrapper classes
int parsedNum = Integer.parseInt("123");
int comparison = Integer.compare(100, 200);

System.out.println("Original number: " + num);
System.out.println("Primitive number: " + primitiveNum);
System.out.println("Parsed number: " + parsedNum);
System.out.println("Comparison result: " + comparison);
}
}

/* Output:

Original number: 100
Primitive number: 100
Parsed number: 123
Comparison result: -1 */

Key Utility Methods

Here are some common utility methods provided by wrapper classes:

  • parseInt(String s): Parses the string argument as a signed decimal integer.
int number = Integer.parseInt("123");
  • valueOf(String s): Returns an Integer object holding the value of the specified string.
Integer number = Integer.valueOf("123");
  • toString(): Returns a string object representing the specified integer.
String str = Integer.toString(123);
  • compare(int x, int y): Compares two int values numerically.
int result = Integer.compare(100, 200);

Advantages of Wrapper Classes

  1. Ease of Use with Collections: Collections such as ArrayList can store wrapper objects.
  2. Support for Null Values: Unlike primitives, wrapper objects can be null, representing the absence of a value.
  3. Utility Methods: Provides many helpful methods for conversion and comparison.

Disadvantages of Wrapper Classes

  1. Performance Overhead: Creating and managing objects involves additional memory and processing time compared to using primitives.
  2. Null Handling: Wrapper classes can introduce NullPointerException if not properly handled.

--

--

Yuvaraj
Yuvaraj

No responses yet