0% found this document useful (0 votes)
68 views6 pages

Java Wrapper Classes Explained

The wrapper classes in Java allow primitive data types to be converted to object references and vice versa. The eight primitive types have corresponding wrapper classes, and since Java 5 autoboxing and unboxing was introduced to automatically convert between primitives and wrappers without needing explicit wrapper class methods. Examples show converting primitives like int to wrappers like Integer, and converting wrappers back to primitives.

Uploaded by

Sheshgiri Sheshu
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)
68 views6 pages

Java Wrapper Classes Explained

The wrapper classes in Java allow primitive data types to be converted to object references and vice versa. The eight primitive types have corresponding wrapper classes, and since Java 5 autoboxing and unboxing was introduced to automatically convert between primitives and wrappers without needing explicit wrapper class methods. Examples show converting primitives like int to wrappers like Integer, and converting wrappers back to primitives.

Uploaded by

Sheshgiri Sheshu
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

Wrapper classes in Java

The wrapper class in Java provides the mechanism to convert primitive into
object and object into primitive.
Since J2SE 5.0, 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.

The eight classes of the [Link] package are known as wrapper classes in
Java. The list of eight wrapper classes are given below:

Primitive Type Wrapper class

boolean Boolean

char Character

byte Byte

short Short

int Integer

long Long

float Float

double Double

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.
Since Java 5, we do not need to use the valueOf() method of wrapper classes
to convert the primitive into objects.

Wrapper class Example: Primitive to Wrapper

//Java program to convert primitive into objects


//Autoboxing example of int to Integer
public class Ex
{
public static void main(String args[])
{
//Converting int into Integer
int a=20;
Integer i=[Link](a);
//converting int into Integer explicitly
Integer j=a;
//autoboxing, now compiler will write [Link](a) internally
[Link](a+" "+i+" "+j);
}
}
Output:
20 20 20

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 Ex
{
public static void main(String args[])
{
//Converting Integer to int
Integer a=new Integer(3);
int i=[Link](); //converting Integer to int explicitly
int j=a; //unboxing, now compiler will write [Link]() internally
[Link](a+" "+i+" "+j);
}
}
Output:
333

Java Wrapper classes Example


//Java Program to convert all primitives into its corresponding

//wrapper objects and vice-versa

public class Ex

public static void main(String args[])

byte b=10;

short s=20;

int i=30;

long l=40;

foat f=50.0F;

double d=60.0D;

char c='a';

boolean b2=true;
//Autoboxing: Converting primitives into objects

Byte byteobj=b;

Short shortobj=s;

Integer intobj=i;

Long longobj=l;

Float foatobj=f;

Double doubleobj=d;

Character charobj=c;

Boolean boolobj=b2;

//Printing objects

[Link]("---Printing object values---");

[Link]("Byte object: "+byteobj);

[Link]("Short object: "+shortobj);

[Link]("Integer object: "+intobj);

[Link]("Long object: "+longobj);

[Link]("Float object: "+foatobj);

[Link]("Double object: "+doubleobj);

[Link]("Character object: "+charobj);

[Link]("Boolean object: "+boolobj);

//Unboxing: Converting Objects to Primitives

byte bytevalue=byteobj;

short shortvalue=shortobj;

int intvalue=intobj;

long longvalue=longobj;

foat foatvalue=foatobj;

double doublevalue=doubleobj;

char charvalue=charobj;
boolean boolvalue=boolobj;

//Printing primitives

[Link]("---Printing primitive values---");

[Link]("byte value: "+bytevalue);

[Link]("short value: "+shortvalue);

[Link]("int value: "+intvalue);

[Link]("long value: "+longvalue);

[Link]("foat value: "+foatvalue);

[Link]("double value: "+doublevalue);

[Link]("char value: "+charvalue);

[Link]("boolean value: "+boolvalue);

Output:

---Printing object values---

Byte object: 10

Short object: 20

Integer object: 30

Long object: 40

Float object: 50.0

Double object: 60.0

Character object: a

Boolean object: true

---Printing primitive values---

byte value: 10

short value: 20

int value: 30

long value: 40
foat value: 50.0

double value: 60.0

char value: a

boolean value: true

Common questions

Powered by AI

To explicitly convert a primitive type to its corresponding wrapper object in Java, you can use the valueOf() method, as shown in the example of converting int to Integer: Integer i = Integer.valueOf(a). To convert a wrapper object to a primitive type, the intValue() method can be used: int i = a.intValue(). However, since Java 5, such explicit conversions are handled automatically by autoboxing and unboxing, so you can directly assign an int to an Integer or vice-versa, and the conversion will happen implicitly .

Wrapper classes in Java allow representation of nullable values in contexts where primitives are used. Unlike primitives, which cannot be null, wrapper classes can be assigned a null value, enabling clearer representation and handling of absent or undefined values. This is particularly important when dealing with fields in Java's API, such as databases and JSON objects, or when returning results where null denotes a meaningful None or absent state, enhancing the program's robustness in handling null scenarios safely .

Java handles equality checks between a primitive type and its corresponding wrapper class by unboxing the wrapper object to its primitive type and then performing the comparison. The equals() method available in wrapper classes compares the actual values encapsulated. For example, when comparing an Integer object to an int, the Integer is unboxed to int, and then the primitive comparison is performed, ensuring compatibility between these types and correctness of comparison outcomes .

Wrapper classes become essential in systems using generics because Java generics do not support primitive types; they work only with Objects. Wrapper classes allow primitives like int, float, etc., to be used with generics by providing an object representation of these data types. This capability is crucial in creating type-safe code within collections like List and Set, where maintaining type safety without converting primitives to objects would be impossible, leading to more robust and error-free code .

In a Java program that involves mathematical operations in collections, not using autoboxing could lead to bugs due to manual conversion mistakes. For instance, consider manually converting an int to an Integer when adding elements to a List<Integer>. Without autoboxing, a developer might forget to perform this conversion or apply it incorrectly by using null-checks or invalid constructor calls, resulting in NullPointerExceptions or ClassCastExceptions. Autoboxing minimizes these errors by ensuring conversions are consistently handled by the compiler .

Wrapper classes are useful beyond autoboxing and unboxing as they provide utility methods for converting to and from strings, parsing values, and performing advanced numerical operations. They can be used in generic collections that require objects, facilitate type safety in collections, and enable functionality such as caching to improve performance. Additionally, they provide constants such as TYPE which represent the class of the corresponding primitive type, and they are often necessary for handling null as a valid value in collections, unlike primitives which cannot be null .

Autoboxing and unboxing provide a simpler and more readable way to convert between primitives and their corresponding wrapper classes without having to explicitly call conversion methods. This feature reduces boilerplate code and potential errors in type conversion as the compiler handles these operations automatically. Moreover, it aligns with Java's type system, allowing primitives and objects to be used interchangeably in contexts such as collections where objects are required, ultimately improving code maintainability and readability .

The wrapper classes in Java are part of the java.lang package and include Boolean, Character, Byte, Short, Integer, Long, Float, and Double. Autoboxing is the automatic conversion of primitive data types into their corresponding wrapper class objects, such as converting an int to an Integer. Unboxing is the reverse process, where an object is automatically converted to its corresponding primitive type, like an Integer to an int. Since Java 5, this conversion happens automatically, without the need to call methods like valueOf() for autoboxing or intValue() for unboxing .

Autoboxing simplifies coding in Java Collections by allowing primitives to be automatically converted to their respective wrapper classes, which are required for Java Collections because they cannot hold primitive types. For example, when adding an int to an ArrayList of Integer, Java automatically boxes the int into an Integer object. List<Integer> list = new ArrayList<>(); list.add(5); Here, the primitive 5 is autoboxed into an Integer object without needing explicit conversion, providing cleaner and more efficient code .

In Java, primitives are stored directly as the value in the stack memory, which is efficient in terms of both speed and space. On the other hand, wrapper classes are objects and are stored on the heap memory, which introduces overhead due to object instantiation and garbage collection. While wrapper objects offer additional functionalities and flexibility, they require more memory and processing resources than primitives, which affects the performance of programs that involve large numbers of objects. Hence, the choice between primitives and wrapper classes can impact an application's memory management and overall performance .

You might also like