Wrapper class in Java

Wrapper class in java is a class that is used to convert primitive data types into objects. We have 8 primitive data types in java which are byte, short, int, Boolean, long, double, float etc. Which are already defined in java. These data types are not objects.  This process is also called autoboxing and it’s vice versa is called unboxing. Wrapper classes are also the subclasses of abstract classes. More memory can be held in the case of wrapper class than primitive data types. We only use primitive data types when we want to have high efficiency but if we want to use objects, we use wrapper class.

Some very useful methods of Integer wrapper class are parseInt() which on applying will return a signed decimal int value equal to string str, toString() will help in returning a new String object representing the integer I, floatValue() will return the value of an Integer as a float value etc.

Use of wrapper class in Java

  • Objects do belong to certain class that’s why wrapper class is more easy to use, for example with <ArrayList>.
  • Wrapper class allow the use of null values.
  •  Java only supports call by value. Hence, the original value does not change if we pass the primitive data types. As a result, it will if we change them into objects.
  • We require the conversion of objects into stream of byte if we want to perform serialization. Hence, primitive data types can be converted to objects with the help of wrapper class to perform serialization.
  • While performing multi-threading in Java, we need objects rather than primitive data types.
  • We can also make custom wrapper class objects in which wrapper class will wrap the primitive data types by performing the activities reserved for objects.
  • Java collection framework also works with objects such as TreeSet ArrayList, LinkedList etc.
  • Java.util.package provides the certainty in java to work with objects in the java classes.

Below is a table for conversion of primitive type to wrapper class:

shortShortIt will Store the pack of whole numbers from -32,768 to 32,767
intIntegerIt will Store the pack of whole numbers from 2,147,483,648 to 2,147,483,647
longLongStores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
floatFloatIt stores fractional numbers and it is sufficient for storing 6 to 7 decimal digits
doubleDoubleThis will store fractional numbers and it is sufficient for storing 15 decimal digits

Look at the code example below for converting primitive data type to wrapper object.

public class Developerhelps {
public static void main(String args[]){  
int value=10;  
Integer obj=Integer.valueOf(value);
System.out.println(value+ " "+ obj);  

The output of the code is:

10    10

Here the data type and object both will have the same values.

Below is another example of how we can create objects in wrapper class:

public class Developerhelps { 
public static void main(String[] args) {
    Integer myInt = 100; 
    Double myDouble = 50.99; 
    Character myChar = 'M'; 

The output of the program will be:


What is an object wrapper?

As we are aware of the fact, that a wrapper class has an object which contains primitive data types. We can also sum it up as the object of wrapper class wraps the primitive values. For example, Boolean is a wrapper class in java which is present in java.lang package. However, string is not a wrapper class in Java because it does not wrap any primitive value around it. There are some data types which stores only objects. Hence, we need wrapper class to convert the other primitive data types into objects. This is how a value is transformed into wrapper object:

HashMap<Int, Str> hm = new HashMap<Int, Str>();

Leave a comment

Your email address will not be published. Required fields are marked *

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!