Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Wrapper Class in Java

Introduction to Wrapper Class in Java

Wrapper Class is an important class of java.lang library. Wrapper Class objects create a wrapper for the primitive data types. While creating an object of wrapper class, space is created in the memory where primitive data type is stored.

Wrapper class provides some features for the conversion of the object to primitive data & primitive data to object i.e. boxing/unboxing. Conversion from objects to primitive data & primitive data to an object takes place automatically. Primitive data type refers to the int, float, char, double, byte, etc.

Syntax: 

Below given declaration shows how a Wrapper class works in the java program.

e.g. int i = 100;

In the below-given example, we can see i is integer data type. Some time in java integer needs to be passed as a type of object. In this case, we can use the wrapper class to convert an integer into an object.

Integer intVal = new Integer(i);

In the above-given syntax, we can see how a primitive data type is being converted to an object using an Integer class object. Also can say primitive data type is wrapped as an object.

Uses of Wrapper Class in Java

  1. Wrapper Class can be used to convert an object into a primitive data type or its vice-versa.
  2. The use of wrapper classes improves the performance of the program.
  3. Wrapper class helps in the serialization of object & its vice versa, It can convert primitive data to objects. Sometimes we need to stream objects, in that case, wrapper class can convert it into serialization format.
  4. Some important methods are provided by the wrapper class, which is used to perform the searching & sorting in the collections.
  5. Subtraction & addition, these types of operations cannot modify the old value of Wrapper class primitive integer that’s why Wrapper class is known as immutable.
  6. Wrapper class used in the multithreading process, as the multithreading process needs an object for the synchronization of processes. Wrapper class converts different data type to an object.

On the basis of JavaAPI, the Wrapper class hierarchy keeps Object at the top of the different primitive classes. Number, Character & Boolean comes at the second level just after the Object. At the third level Byte, Short, Int, Long, Float, Double comes under the Number data type.

Wrapper classes use the following two mechanisms Autoboxing & unboxing for the conversion/wrapping of the data type or conversion of an object into the primitive data type.

Autoboxing: Autoboxing refers to the automatic conversion of the primitive data type to object using Wrapper classes. It is known as Autoboxing. In the below-given Examples, int converted to Integer object & in examples c, different primitive data type is being converted to the corresponding object.

Unboxing: Unboxing is the reverse process of Autoboxing. Automatically converting wrapper class object to the corresponding primitive data type is known as Unboxing. In the below-given example b, the Integer object is being converted to int primitive data type.

Examples of Wrapper Class in Java

Below are the different examples of Wrapper Class in Java.

Example #1

In the below-given example, we can see how manual conversion takes place through wrapper class from int i to an object k.

Code:

import java.util.*;
class WrapperExample {
public static void main(String args[]){
int j=100;
//converting int j to integer k as an object
Integer k = new Integer(j);
System.out.println(j + "\n" + k);
}
}

Output:

In the above-given example, we can see how conversion takes place explicitly.

Example #2

In the below-given example, we can see this process of conversion sometimes takes place automatically i.e. known as autoboxing.

Code:

import java.util.*;
class AutoboxingUnboxingExample {
public static void main(String args[]){
int j = 500;
ArrayList arrValues = new ArrayList();
arrValues.add(j);  // autoboxing takes place implicitly
System.out.println(arrValues.get(0));
}
}

Output:

In the above-given example, int value converted to object implicitly as an object. Further, this value can get from the ArrayList.

Example #3

In this example, we will go through the implementation of the Unboxing. Unboxing is the reverse process of Autoboxing.

Code:

import java.util.*;
class AutoboxingUnboxingExample {
public static void main(String args[]){
ArrayList arrValues = new ArrayList();
arrValues.add(250);
//unboxing here as int data type from Integer object
int k = arrValues.get(0);
//value printed is in primitive data type
System.out.println(k);
}
}

Output:

 

In the above-given example, the ArrayList object field is converted into k primitive data type i.e. int k.

Example #4

The following given example have all the details of Autoboxing & Unboxing.

Code:

import java.util.*;
class WrapperConversionExample {
public static void main(String args[]){
int i = 15;
float j = 9.6f;
double k = 120.8;
byte l = 1;
//creating instance of Integer object
Integer iObj = new Integer(i);
//creating instance of Float object
Float fObj = new Float(j);
//creating instance of Double object
Double dObj = new Double(k);
//creating instance of Double object
Byte bObj = new Byte(l);
//value printed is in object
System.out.println("Value as an Integer object > " + iObj);
System.out.println("Value as a Float object > " + fObj);
System.out.println("Value as a Double object > " + dObj);
System.out.println("Value as a Byte object > " + bObj);
//primitive data type from the object
int m = iObj;
float n = fObj;
double o = dObj;
byte p = bObj;
//value printed is in primitive data type
System.out.println("Value as an int primitive type > " + m);
System.out.println("Value as a float primitive type > " + n);
System.out.println("Value as a double primitive type > "+ o);
System.out.println("Value as a byte primitive type > " + p);
}
}

Output:

In the above-given program, we can see the implementation of Wrapper classes. Wrapper classes are converting the primitive data type to object & object to the primitive data type. The wrapper class provides separate classes for each primitive data type.

Conclusion

Through the Wrapper classes, we can easily understand the autoboxing & unboxing. How conversion takes place from primitive to object & its vice versa which can be easily understood through Wrapper classes. For each of the primitive data types, there is a dedicated class in java.

Recommended Article

This is a guide to Wrapper Class in Java. Here we discuss syntax and uses of the wrapper class in Java along with different examples and code implementation. You can also go through our other suggested articles to learn more –

  1. How does Case Statement in Java?
  2. What is Generics in Java?
  3. The Concept of Serialization in Java
  4. Java Top 8 Interview Questions

The post Wrapper Class in Java appeared first on EDUCBA.



This post first appeared on Best Online Training & Video Courses | EduCBA, please read the originial post: here

Share the post

Wrapper Class in Java

×

Subscribe to Best Online Training & Video Courses | Educba

Get updates delivered right to your inbox!

Thank you for your subscription

×