Wrapper Classes in Java
×


Wrapper Classes in Java

240

What are Wrapper Classes in Java?

In Java, wrapper classes provide a way to use primitive data types like (int, double, boolean, etc.) as objects. Since primitives are not objects, wrapper classes wrap these primitives inside an object so they can be used where objects are required, such as in collections like ArrayList.

Why do we need Wrapper Classes?

Java is an object-oriented language, and many APIs work with objects. However, primitives are not objects, so wrapper classes come to the rescue by allowing primitive data types to be treated as objects. This helps with:

  • Storing primitive values in collection classes (e.g., ArrayList only works with objects)
  • Using utility methods provided by wrapper classes
  • Performing conversions between strings and primitive values

List of Wrapper Classes

Here’s a quick list of Java primitive types and their corresponding wrapper classes:

  • intInteger
  • doubleDouble
  • charCharacter
  • booleanBoolean
  • byteByte
  • shortShort
  • longLong
  • floatFloat

Autoboxing and Unboxing

Java automatically converts between primitives and their wrapper objects through a feature called autoboxing and unboxing.

Autoboxing is the automatic conversion of a primitive to its wrapper object, while unboxing is converting the wrapper back to a primitive.

Example:

Integer wrappedInt = 10;   // Autoboxing: int → Integer
int primitiveInt = wrappedInt;  // Unboxing: Integer → int

Commonly Used Methods in Wrapper Classes

Wrapper classes come with useful methods. Here are some popular ones:

  • parseXxx(String s) — converts a string to a primitive (e.g., Integer.parseInt("123"))
  • valueOf(String s) — converts a string to a wrapper object (e.g., Boolean.valueOf("true"))
  • toString() — converts the wrapper object to a string

Example:

String numberStr = "50";
int num = Integer.parseInt(numberStr); // converts string to int

Boolean flag = Boolean.valueOf("true"); // converts string to Boolean object

String str = num + ""; // converts int to string (simple concatenation)

Why Not Just Use Primitives?

Primitives are more efficient in terms of memory and performance, so it’s best to use them when you don’t need object features.

However, wrapper classes are essential when:

  • You need to store data in Java Collections like ArrayList, HashMap, etc.
  • You need to call methods that require objects
  • You want to use built-in utility methods

Example: Using Wrapper Classes in Collections

Here’s an example showing why wrapper classes are necessary with collections:

import java.util.ArrayList;

public class WrapperExample {
    public static void main(String[] args) {
        // ArrayList cannot hold primitives directly
        ArrayList numbers = new ArrayList<>();

        // Autoboxing converts int to Integer automatically
        numbers.add(5);
        numbers.add(10);

        // Accessing values (unboxing happens automatically)
        int first = numbers.get(0);
        System.out.println("First number: " + first);
    }
}

Summary

Wrapper classes in Java provide an object representation for all primitive types. They enable primitives to be used where objects are required, facilitate autoboxing/unboxing, and offer utility methods for conversions and manipulations.

While primitives are more lightweight, wrapper classes are essential when working with collections or APIs that demand objects.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat