Wrapper Classes in Java

Introduction

 
In Java, everything is related to an object. Wrapper classes in Java convert data types into objects since primitive data types are not objects. So to convert them into objects we need wrapper classes in Java.
 

Wrapper Classes in Java

 
Wrapper classes are used to convert data types into objects. As the name suggests, a wrapper class wraps a data type and provides it an object scenario. Wrapper classes can also unwrap the specific object and return the data type. These wrapper classes are in the java.lang package. We can also use an example of a toffee. It is wrapped in a wrapper to prevent it from dust. The consumer takes the toffee, unwrap it and eat it.
 
int a = 50;
Integer ir = new Integer(a);
 
In the code above the int data type, a is converted into an object.
 
int b = ir.intValue();
System.out.println(b);//prints 50
 
The code above is used to unwrap the object.
 

List of Wrapper Classes in Java 

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

Hierarchy of Wrapper Classes

 
hierarchy of wrapper class
 
Example
  1. package demo;    
  2. public class Demo {    
  3.  public static void main(String[] args) {    
  4.   byte rank = 1;    
  5.   int percentage = 95;    
  6.   float range = 11.6 f;    
  7.   double limit = 10.5;    
  8.   Byte b1 = new Byte(rank);    
  9.   Integer i1 = new Integer(percentage);    
  10.   Float f1 = new Float(range);    
  11.   Double d1 = new Double(limit);    
  12.   System.out.println("wrapper objects");    
  13.   System.out.println("Byte object g1: " + b1);    
  14.   System.out.println("Integer object m1 " + i1);    
  15.   System.out.println("Float object f1: " + f1);    
  16.   System.out.println("Double object r1:" + d1);    
  17.   System.out.println("\n");    
  18.   byte b = b1.byteValue();    
  19.   int i = i1.intValue();    
  20.   float f = f1.floatValue();    
  21.   double d = d1.doubleValue();    
  22.   System.out.println("Unwrapped values");    
  23.   System.out.println("byte value, b:" + b);    
  24.   System.out.println("int value, i:" + i);    
  25.   System.out.println("float value, f:" + f);    
  26.   System.out.println("double value, d:" + d);    
  27.  }    
  28. }    
Output
 
 wrapper class
 

Another Use of Wrapper Classes

 
In Java, wrapper classes are also used to convert strings into data types.
 
Example
  1. package demo;  
  2. public class Demo  
  3. {  
  4.     public static void main(String args[])  
  5.     {  
  6.         String cost ="100";  
  7.         String percentage = "5.8f";  
  8.         String fine ="50.2";  
  9.         int x = Integer.parseInt(cost);  
  10.         float f = Float.parseFloat(percentage);  
  11.         double d = Double.parseDouble(fine);    
  12.         System.out.println("\nAfter parsing");  
  13.         System.out.println("int value: " + x);  
  14.         System.out.println("float value: " + f);  
  15.         System.out.println("double value: " + d);  
  16.         Integer ir = new Integer(cost);  
  17.         Float ft = new Float(percentage);  
  18.         Double dl = new Double(fine);  
  19.         int ir1 = ir.intValue();  
  20.         float ft1 = ft.floatValue();  
  21.         double dl1= dl.doubleValue();    
  22.         System.out.println("\nAfter conversion");  
  23.         System.out.println("int value: " + ir1);  
  24.         System.out.println("float value: " + ft1);  
  25.         System.out.println("double value: " + dl1);  
  26.     }  
  27. }   
Output
 
string to data type
 
 
 

Summary

 
This article explains wrapper classes in Java.