Wrapper Classes In Java

In Java, the wrapper class facilitates the conversion of primitive data types to objects and vice versa.

Starting from J2SE 5.0 onwards, the autoboxing and unboxing functionality automatically transforms primitive types into objects and vice versa. Autoboxing refers to the automatic conversion of primitives into objects, while unboxing involves the conversion of objects back into primitives.

Advantages of Wrapper Classes

Java Wrapper classes offer several advantages over primitive data types. It includes encapsulation, type conversion, autoboxing and unboxing, and utility methods. Wrapper classes are also thread-safe and immutable, making them ideal for multi-threaded applications. Some advantages of wrapper class are:

  • Collections allowed only object data.
  • On object data we can call multiple methods compareTo, equals, toString
  • Cloning process only objects
  • Object data allowed null values.
  • Serialization can allow only object data.
  • Significance of Wrapper Class in Java

  • Encapsulation
  • Type Conversion
  • Utility Methods
  • Null Handling
  • Use of Wrapper Classes in Java

Java is an object-oriented programming language, so we need to deal with objects many times like in Collections, Serialization, Synchronization, etc. Let us see the different scenarios, where we need to use the wrapper classes.

  • Change the value in Method: Java supports only call by value. So, if we pass a primitive value, it will not change the original value. But, if we convert the primitive value in an object, it will change the original value.
  • Serialization: We need to convert the objects into streams to perform the serialization. If we have a primitive value, we can convert it in objects through the wrapper classes.
  • Synchronization: Java synchronization works with objects in Multithreading.
  • java.util package: The java.util package provides the utility classes to deal with objects.
  • Collection Framework: Java collection framework works with objects only. All classes of the collection framework (ArrayList, LinkedList, Vector, HashSet, LinkedHashSet, TreeSet, PriorityQueue, ArrayDeque, etc.) deal with objects only.

The group of eight classes in the java.lang package is recognized as wrapper classes within the Java programming language. Below is the enumeration of these eight wrapper classes:

Primitive Type Wrapper class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double

Autoboxing

Autoboxing refers to the automatic transformation of primitive data types into their respective wrapper classes. This includes conversions such as 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.

Starting from Java 5, there is no requirement to utilize the valueOf method from wrapper classes for converting primitives into objects.

Wrapper Class Example: Primitive to Wrapper

Example

Example

//Java program to convert primitive into objects

//Autoboxing example of int to Integer

public class WrapperExample1{

public static void main(String args[]){

//Converting int into Integer

int a=20;

Integer i=Integer.valueOf(a);//converting int into Integer explicitly

Integer j=a;//autoboxing, now compiler will write Integer.valueOf(a) internally



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

}}

Output:

Output

20 20 20

Unboxing

Unboxing refers to the automatic conversion of a wrapper type to its associated primitive type, serving as the opposite operation of autoboxing. Starting from Java 5, there is no necessity to employ the intValue function from wrapper classes for the conversion of wrapper types to primitive types.

Wrapper Class Example: Wrapper to Primitive

File Name: WrapperExample2.java

Example

//Java program to convert object into primitives

//Unboxing example of Integer to int

public class WrapperExample2{  

public static void main(String args[]){  

//Converting Integer to int  

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);  

}}

Output:

Java Wrapper Classes Example

File Name: WrapperExample3.java

Example

//Java Program to convert all primitives into its corresponding 

//wrapper objects and vice-versa

public class WrapperExample3{

public static void main(String args[]){

byte b=10;

short s=20;

int i=30;

long l=40;

float 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 floatobj=f;

Double doubleobj=d;

Character charobj=c;

Boolean boolobj=b2;



//Printing objects

System.out.println("---Printing object values---");

System.out.println("Byte object: "+byteobj);

System.out.println("Short object: "+shortobj);

System.out.println("Integer object: "+intobj);

System.out.println("Long object: "+longobj);

System.out.println("Float object: "+floatobj);

System.out.println("Double object: "+doubleobj);

System.out.println("Character object: "+charobj);

System.out.println("Boolean object: "+boolobj);



//Unboxing: Converting Objects to Primitives

byte bytevalue=byteobj;

short shortvalue=shortobj;

int intvalue=intobj;

long longvalue=longobj;

float floatvalue=floatobj;

double doublevalue=doubleobj;

char charvalue=charobj;

boolean boolvalue=boolobj;



//Printing primitives

System.out.println("---Printing primitive values---");

System.out.println("byte value: "+bytevalue);

System.out.println("short value: "+shortvalue);

System.out.println("int value: "+intvalue);

System.out.println("long value: "+longvalue);

System.out.println("float value: "+floatvalue);

System.out.println("double value: "+doublevalue);

System.out.println("char value: "+charvalue);

System.out.println("boolean value: "+boolvalue);

}}

Output:

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

float value: 50.0

double value: 60.0

char value: a

boolean value: true

Custom Wrapper Class in Java

Wrapper classes in Java encapsulate primitive data types, giving rise to their name. It is possible to develop a class that encapsulates a primitive data type, thereby enabling the creation of a personalized wrapper class in Java.

File Name: TestC# Tutorial.java

Example

//Creating the custom wrapper class

class C# Tutorial{

private int i;

C# Tutorial(){}

C# Tutorial(int i){

this.i=i;

}

public int getValue(){

return i;

}

public void setValue(int i){

this.i=i;

}

@Override

public String toString() {

  return Integer.toString(i);

}

}

//Testing the custom wrapper class

public class TestC# Tutorial{

public static void main(String[] args){

C# Tutorial j=new C# Tutorial(10);

System.out.println(j);

}}

Output:

Considerations Java Wrapper Classes Vs. Primitive Types

Explore factors such as memory consumption, efficiency, and the optimal scenarios for selecting one option over the other.

Wrapper Classes Primitive Types Considerations
Memory Usage Low High due to lack of overhead
Performance Moderate High because of fewer instructions
When to Use When Encapsulation is needed or when working with complex APIs When Performance and Memory are a major concern
Security High Low as primitives are not immutable
Thread Safety Yes No

Considerations

High due to lack of overhead

High because of fewer instructions

When Performance and Memory are a major concern

Low as primitives are not immutable

Java Wrapper Class MCQ

1) Which of the following is a valid way to create an Integer object with the value 10?

  • Integer val = new Integer(10);
  • Integer val = Integer.valueOf(10);
  • Integer val = 10;
  • int val = 10;

The valueOf function in the Integer class is a static method that is utilized to generate an Integer instance with a specific value.

2) What is the purpose of the intValue method in the Integer wrapper class?

  • It returns the value of the Integer object as an int primitive.
  • It returns the hash code of the Integer object.
  • It compares two Integer objects for equality.
  • It converts the Integer object to its binary representation.

The method intValue retrieves the integer value stored in an Integer object and returns it as a primitive data type of int.

3) Which are the constant fields available a Wrapper Class object in Java?

  • BYTES, SIZE
  • MAXVALUE, MAXEXPONENT
  • MINVALUE, MINEXPONENT
  • All the above

The constants mentioned above are exclusively accessible to non-boolean wrapper classes. MINEXPONENT and MAXEXPONENT, on the other hand, are specifically accessible to the Float and Double classes.

4) What is the purpose of autoboxing in Java?

  • Converting an object to its corresponding primitive data type.
  • Converting a primitive data type to its corresponding object.
  • Converting between different types of wrapper classes.
  • Converting a string to its numeric equivalent.

Autoboxing refers to the automatic conversion of primitive data types into their respective wrapper classes.

5) Which of the following methods can be used to parse a string representation of a number into its corresponding primitive data type?

  • Integer.parseInt
  • Integer.valueOf
  • Integer.intValue
  • Integer.parse

The parseInt function, belonging to the Integer class, is utilized for converting a string that represents an integer into its respective primitive data type.

Input Required

This code uses input(). Please provide values below: