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'; 
    System.out.println(myInt.intValue());
    System.out.println(myDouble.doubleValue());
    System.out.println(myChar.charValue());
  } 
}

The output of the program will be:

100
50.99
M

Leave a comment

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