0% found this document useful (0 votes)
17 views9 pages

Wrapper Classes

The document discusses Java wrapper classes, which allow primitive data types to be used as objects. It explains that wrapper classes provide automatic conversion between primitives and their corresponding wrapper class objects through autoboxing and unboxing. Various wrapper classes are listed along with their corresponding primitive types, and examples are provided of autoboxing primitive values to wrapper objects and unboxing wrapper objects back to primitives.

Uploaded by

clashbros563
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

Wrapper Classes

The document discusses Java wrapper classes, which allow primitive data types to be used as objects. It explains that wrapper classes provide automatic conversion between primitives and their corresponding wrapper class objects through autoboxing and unboxing. Various wrapper classes are listed along with their corresponding primitive types, and examples are provided of autoboxing primitive values to wrapper objects and unboxing wrapper objects back to primitives.

Uploaded by

clashbros563
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Wrapper classes in Java

The wrapper class in Java provides the mechanism to convert


primitive into object and object into primitive.

Autoboxing and unboxing feature convert primitives into objects and


objects into primitives automatically. The automatic conversion of
primitive into an object is known as autoboxing and vice-versa
unboxing.
Java Wrapper Classes
Wrapper classes provide a way to use primitive data types (int, boolean, etc..) as objects.

The table below shows the primitive type and the equivalent wrapper class:
Autoboxing

The automatic conversion of primitive data type into its corresponding wrapper class is known as
autoboxing, for example, byte to Byte, char to Character, int to Integer, long to Long, float to Float,
boolean to Boolean, double to Double, and short to Short.

To create a wrapper object, use the wrapper class instead of the primitive type. To get
the value, you can just print the object:

public class Main {


public static void main(String[] args) {
Integer myInt = 5; 5
Double myDouble = 5.99;
Character myChar = 'A';
5.99
System.out.println(myInt); A
System.out.println(myDouble);
System.out.println(myChar);
}
}
Unboxing
The automatic conversion of wrapper type into its corresponding primitive type is known as
unboxing. It is the reverse process of autoboxing. Since Java 5, we do not need to use the intValue()
method of wrapper classes to convert the wrapper type into primitives.

Wrapper class Example: Wrapper to Primitive

//Java program to convert object into primitives


//Unboxing example of Integer to int
public class WrapperExample2{ Output
public static void main(String args[]){
//Converting Integer to int 333
Integer a=new Integer(3);
int i=a.intValue();//converting Integer to int explicitly
int j=a;//unboxing, now compiler will write a.intValue() internally

System.out.println(a+" "+i+" "+j);


}}
Java Wrapper classes Example
Output
Class Test

1. Primitive Types to Wrapper Objects

If a is equal to 5 and b is equal to 5.65, convert this to wrapper objects.

2. Wrapper Objects into Primitive Types

If a is equal to 23 and b is equal to 5.55, convert this to Primitive Types.

You might also like