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

package demo;

public class Demo

{

    public static void main(String[] args)

    {

        byte rank = 1;

        int percentage = 95;

        float range = 11.6f;

        double limit = 10.5;

        Byte b1 = new Byte(rank);

        Integer i1 = new Integer(percentage);

        Float f1 = new Float(range);

        Double d1 = new Double(limit);

        System.out.println("wrapper objects");

        System.out.println("Byte object g1: "+ b1);

        System.out.println("Integer object m1 "+ i1);

        System.out.println("Float object f1: "+ f1);

        System.out.println("Double object r1:"+ d1);

        System.out.println("\n");

        byte b = b1.byteValue();

        int i = i1.intValue();

        float f = f1.floatValue();

        double d = d1.doubleValue();

        System.out.println("Unwrapped values");

        System.out.println("byte value, b:"+ b);

        System.out.println("int value, i:"+ i);

        System.out.println("float value, f:"+ f);

        System.out.println("double value, d:"+ d);

    }

}

 

Output


wrapper class

 

Another Use of Wrapper Classes

 

In Java, wrapper classes are also used to convert strings into data types.

 

Example

 

package demo;

public class Demo

{

    public static void main(String args[])

    {

        String cost ="100";

        String percentage = "5.8f";

        String fine ="50.2";

        int x = Integer.parseInt(cost);

        float f = Float.parseFloat(percentage);

        double d = Double.parseDouble(fine);  

        System.out.println("\nAfter parsing");

        System.out.println("int value: " + x);

        System.out.println("float value: " + f);

        System.out.println("double value: " + d);

        Integer ir = new Integer(cost);

        Float ft = new Float(percentage);

        Double dl = new Double(fine);

        int ir1 = ir.intValue();

        float ft1 = ft.floatValue();

        double dl1= dl.doubleValue();  

        System.out.println("\nAfter conversion");

        System.out.println("int value: " + ir1);

        System.out.println("float value: " + ft1);

        System.out.println("double value: " + dl1);

    }

}

 

Output


string to data type

 

Summary

 

This article explains wrapper classes in Java.

Up Next
    Ebook Download
    View all
    Learn
    View all