The Wayback Machine - https://web.archive.org/web/20240623223242/https://www.geeksforgeeks.org/need-of-wrapper-classes-in-java/
Open In App

Need of Wrapper Classes in Java

Last Updated : 13 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Firstly the question that hits the programmers is when we have primitive data types then why does there arise a need for the concept of wrapper classes in java. It is because of the additional features being there in the Wrapper class over the primitive data types when it comes to usage. These methods include primarily methods like valueOf(), parseInt(), toString(), and many more.

A wrapper class wraps (encloses) around a data type and gives it an object appearance. Wrapper classes are final and immutable. Two concepts are there in the wrapper classes namely autoboxing and unboxing. 

Autoboxing is a procedure of converting a primitive value into an object of the corresponding wrapper class. For example, converting int to Integer class. The Java compiler applies autoboxing when a primitive value is:

  • Passed as a parameter to a method that expects an object of the corresponding wrapper class.
  • Assigned to a variable of the corresponding wrapper class.

Unboxing is a procedure of converting an object of a wrapper type to its corresponding primitive value. For example conversion of Integer to int. The Java compiler applies to unbox when an object of a wrapper class is:

  • Passed as a parameter to a method that expects a value of the corresponding primitive type.
  • Assigned to a variable of the corresponding primitive type.

Autoboxing and unboxing are pictorially depicted below:

Now let us land on discussing the useful features of wrapper classes, they are listed as follows: 

  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 handles only objects and hence wrapper classes help in this case also.
  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.

One of the major important features provided by wrapper classes is a lot of utility methods. Say when we have a float value, and we want to find the integer value of that float, then we have a specific method for that which is depicted from the illustration given below.

Illustration:

If we want to create an integer value from a string or a boolean value from a string. We can do it with the help of wrapper classes.

Syntax: Creation from other data types

Integer hundred = Integer.valueOf("100");
Boolean value = Boolean.valueOf("True");

 Simple Autoboxing of wrapper class example 

Java




import java .util.*;
class gfg {
 
    public static void main(String args[]) {
        int x = 5;
        float y = 3.14f;
        long z=6000;
 
        // autoboxing
        Integer intobj = x;
        Float floatobj=y;
        Long longobj=z;
        // print
        System.out.println(intobj);
        System.out.println(floatobj);
        System.out.println(longobj);
    }
}


Output

5
3.14
6000

Example:

Java




// Java Program to Show Wrapper class concept
 
// Importing input output classes
import java.io.*;
 
// Main Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // In java, in case of floating values
        // they are stored as x = (y)f
 
        // Conversion of float value to int
        Float floatWrap = Float.valueOf(45.158f);
 
        // Invoking the intValue() method over the stored
        // float value to store
        int floatToInt = floatWrap.intValue();
 
        // Print the non-primitive(Integer) value
        System.out.println(floatToInt);
 
        // Now for another number N
        // Say N = 5
 
        // Convert the binary number to the integer value
        Integer five = Integer.valueOf("101", 2);
 
        // Print the number
        System.out.println(five);
    }
}


Output

45
5


Previous Article
Next Article

Similar Reads

Utility Methods of Wrapper Classes in Java
Prerequisite: Wrapper Classes The objective of the Wrapper class is to define several utility methods which are required for the primitive types. There are 4 utility methods for primitive type which is defined by the Wrapper class: 1. valueOf() method: We can use the valueOf() method to create a Wrapper object for a given primitive or String. There
4 min read
Sorting Elements of Arrays and Wrapper Classes that Already Implements Comparable in Java
Java provides the Comparable interface to sort objects using data members of the class. The Comparable interface contains only one method compareTo() that compares two objects to impose an order between them. It returns a negative integer, zero, or positive integer to indicate if the input object is less than, equal to, or greater than the current
4 min read
Different Ways to Create the Instances of Wrapper Classes in Java
Wrapper Class a class 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. Methods: We can use two ways to construct the instance of the Wrapper Classes Usi
5 min read
Wrapper Classes in Java
A Wrapper class in Java is a class 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. Let's check on the wrapper classes in Java with examples: Need of Wr
6 min read
Primitive Wrapper Classes are Immutable in Java
In Java, an immutable class is a class (Integer, Byte, Long, Float, Double, Character, Boolean, and Short) which once created then its body can not be changed and the same applies to immutable objects which once created cannot be changed. Now the question arises that we do need the help of wrapper classes while dealing with primitive data types in
3 min read
Java Program to Convert Binary String to Decimal Using Wrapper Class
Given a binary string as input, we need to write a program to convert the given binary string into its equivalent decimal number. Examples: Input : 1111 Output : 15 Input : 1001101 Output : 77 Input : 1101 Output : 13 The idea is to extract each character of a given binary string using charAt() and convert those characters into their corresponding
2 min read
Java I/O Operation - Wrapper Class vs Primitive Class Variables
It is better to use the Primitive Class variable for the I/O operation unless there is a necessity of using the Wrapper Class. In this article, we can discuss briefly both wrapper class and primitive data type. A primitive data type focuses on variable values, without any additional methods.The Default value of Primitive class variables are given b
3 min read
Compute modulus division by a power-of-2-number using Wrapper Class
Prerequisite : Compute modulus division by a power-of-2-number Now as you know for getting n modulus 2k, we just need to return k bits(from LSB) in binary representation of n. In Java, you can use Wrapper class toBinaryString() method to get binary string representation of a number and getting substring from (str.length()-k) to end. And then by usi
2 min read
Need of Concurrent Collections in java
As we already know Collections which is nothing but collections of Objects where we deals with the Objects using some pre-defined methods. But There are several problems which occurs when we use Collections concept in multi-threading. The problems which occurs while using Collections in Multi-threaded application: Most of the Collections classes ob
2 min read
Java Servlets | Need of Filters
The need for implementing filters can be understood with the help of few examples Let's take example of a Web application that formats the data to be presented to clients in a specific format say Excel However, at a later point of time, the clients may require data in some other format, such as Hypertext Markup Language (HTML) Portable Document For
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg