0% found this document useful (0 votes)
5 views

Wrapper Class

A Wrapper class in Java encapsulates primitive data types, allowing them to be treated as objects. Wrapper classes are essential for converting primitives to objects, enabling their use in collections and synchronization in multithreading. Autoboxing and unboxing facilitate the automatic conversion between primitive types and their corresponding wrapper classes.

Uploaded by

sahilshedke
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Wrapper Class

A Wrapper class in Java encapsulates primitive data types, allowing them to be treated as objects. Wrapper classes are essential for converting primitives to objects, enabling their use in collections and synchronization in multithreading. Autoboxing and unboxing facilitate the automatic conversion between primitive types and their corresponding wrapper classes.

Uploaded by

sahilshedke
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Wrapper Class

A Wrapper class in Java is one whose object wraps or contains primitive data
types. When we create an object to a wrapper class, it contains a field and in this
field, we can store primitive data types. In other words, we can wrap a primitive
value into a wrapper class object.

Need of Wrapper Classes


There are certain needs for using the Wrapper class in Java as mentioned below:
1. They convert primitive data types into objects. Objects are needed if we wish to
modify the arguments passed into a method (because primitive types are
passed by value).
2. The classes in java.util package handle only objects and hence wrapper classes
help in this case.
3. Data structures in the Collection framework, such as ArrayList and Vector, store
only objects (reference types) and not primitive types.
4. An object is needed to support synchronization in multithreading.

Advantages of Wrapper Classes


1. Collections allow only object data.
2. On object data we can call multiple methods compareTo(), equals(), toString()
3. The cloning process only works on objects
4. Object data allows null values.
5. Serialization allows only object data.

Primitive Data Types and their Corresponding


Wrapper Class
Primitive Data Type Wrapper Class

char Character

byte Byte

short Short

int Integer

long Long

float Float

double Double

boolean Boolean
Autoboxing and Unboxing

1. Autoboxing
The automatic conversion of primitive types to the object of their corresponding
wrapper classes is known as autoboxing. For example – conversion of int to
Integer, long to Long, double to Double, etc.

Example:
// Java program to demonstrate Autoboxing

import java.util.ArrayList;
class Autoboxing {
public static void main(String[] args)
{
char ch = 'a';

// Autoboxing- primitive to Character object


// conversion
Character a = ch;

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

// Autoboxing because ArrayList stores only objects


arrayList.add(25);

// printing the values from object


System.out.println(arrayList.get(0));
}
}

2. Unboxing

It is just the reverse process of autoboxing. Automatically converting an object of a


wrapper class to its corresponding primitive type is known as unboxing. For
example – conversion of Integer to int, Long to long, Double to double, etc.

Example:
// Java program to demonstrate Unboxing

import java.util.ArrayList;

class Unboxing {
public static void main(String[] args) {
Character ch = 'a';
// unboxing - Character object to primitive conversion
char a = ch;
ArrayList<Integer> arrayList = new ArrayList<Integer>();
arrayList.add(24);

// unboxing because get method returns an Integer object


int num = arrayList.get(0);

// printing the values from primitive data types


System.out.println(num);
}
}

Java Wrapper Classes Example


// Java program to demonstrate Wrapping and UnWrapping in Classes
import java.io.*;

class GFG {
public static void main(String[] args)
{
// byte data type
byte a = 1;

// wrapping around Byte object


Byte byteobj = new Byte(a);
// Use with Java 9
// Byte byteobj = Byte.valueOf(a);

// int data type


int b = 10;

// wrapping around Integer object


Integer intobj = new Integer(b);
// Use with Java 9
// Integer intobj = Integer.valueOf(b);

// float data type


float c = 18.6f;

// wrapping around Float object


Float floatobj = new Float(c);
// Use with Java 9
// Float floatobj = Float.valueOf(c);

// double data type


double d = 250.5;

// Wrapping around Double object


Double doubleobj = new Double(d);
// Use with Java 9
// Double doubleobj = Double.valueOf(d);

// char data type


char e = 'a';

// wrapping around Character object


Character charobj = e;

// printing the values from objects


System.out.println(
"Values of Wrapper objects (printing as objects)");
System.out.println("\nByte object byteobj: "
+ byteobj);
System.out.println("\nInteger object intobj: "
+ intobj);
System.out.println("\nFloat object floatobj: "
+ floatobj);
System.out.println("\nDouble object doubleobj: "
+ doubleobj);
System.out.println("\nCharacter object charobj: "
+ charobj);

// objects to data types (retrieving data types from


// objects) unwrapping objects to primitive data
// types
byte bv = byteobj;
int iv = intobj;
float fv = floatobj;
double dv = doubleobj;
char cv = charobj;

// printing the values from data types


System.out.println(
"\nUnwrapped values (printing as data types)");
System.out.println("\nbyte value, bv: " + bv);
System.out.println("\nint value, iv: " + iv);
System.out.println("\nfloat value, fv: " + fv);
System.out.println("\ndouble value, dv: " + dv);
System.out.println("\nchar value, cv: " + cv);
}
}

You might also like