Cyberithub

Best Explanation of Wrapper Classes in Java: Autoboxing and Unboxing(v1.8) with Examples

Advertisements

In this tutorial, we will look into the concepts of Wrapper Class in Java along with the mechanism of Autoboxing and Unboxing with examples. The wrapper classes are a place holder for a lot of static members to handle the primitive types. A few common members for all the wrapper classes are described below:

All wrapper classes have a public static final variable called TYPE which is of type Class. This variable is the Class object for the corresponding primitive type. Byte.TYPE is the same as byte.class. We have one more class which is not considered as a wrapper class but has this variable called TYPE. The Void class is not a wrapper class since it does not encapsulate any value.

All the wrapper classes also have another static and final variable of type int called SIZE, which gives the size of the corresponding primitive type in bytes, e.g. Integer.SIZE is the value 4. More on The Class of Java.

Best Explanation of Wrapper Classes in Java: Autoboxing and Unboxing(v1.8) with Examples 2

Wrapper Classes

Also Read: 15 ansible-vault command examples to encrypt and decrypt sensitive data/files on Linux

It is used to convert primitive into object and vice-versa. There are total of eight classes of java.lang package which are known as wrapper class in Java. It contains two different features or mechanisms:-

  • Autoboxing
  • Unboxing

Below are the Primitive Type and their Wrapper Class.

Primitive TypeWrapper Class
booleanBoolean
charCharacter
byteByte
shortShort
int Integer
longLong
floatFloat
doubleDouble

Few of the features of Wrapper Classes are :-

  • Each of the numeric type-wrapper classes - Byte, Short, Integer, Long, Float and Double extends class Number.
  • The type-wrapper classes are final classes hence it cannot be extended.
  • Primitive types do not have methods, so the methods related to a primitive type are located in the corresponding type-wrapper class (e.g., method parseInt, which converts a String to an int value, is located in class Integer).

Autoboxing

It is a mechanism to convert Primitive to Object or in other words the boxing conversion is the conversion of a primitive value to its wrapper type. The feature of automatic conversion between primitive data types and their corresponding wrapper classes are available from Java 5 onwards.

Example 1

[root@localhost wrapper]# vi autoboxing.java
import java.lang.Integer;

class autoboxing
{
public static void main(String args[])
{
int a = 10;
Integer i = new Integer(a);
System.out.println(i);
}
}

Compile Your Program

[root@localhost wrapper]# javac autoboxing.java

Output

[root@localhost wrapper]# java autoboxing
10

Example 2

[root@localhost wrapper]# vi autoboxing.java
import java.lang.Character;

class autoboxing
{
public static void main(String args[])
{
char a = 'c';
Character i = new Character(a);
System.out.println(i);
}
}

Compile Your Program

[root@localhost wrapper]# javac autoboxing.java

Output

[root@localhost wrapper]# java autoboxing
c

Example 3

[root@localhost wrapper]# vi autoboxing.java
import java.lang.Byte;

class autoboxing
{
public static void main(String args[])
{
byte a = 10;
Byte i = new Byte(a);
System.out.println(i);
}
}

Compile Your Program

[root@localhost wrapper]# javac autoboxing.java

Output

[root@localhost wrapper]# java autoboxing
10

Example 4

[root@localhost wrapper]# vi autoboxing.java
import java.lang.Short;

class autoboxing
{
public static void main(String args[])
{
short a = 10;
Short i = new Short(a);
System.out.println(i);
}
}

Compile Your Program

[root@localhost wrapper]# javac autoboxing.java

Output

[root@localhost wrapper]# java autoboxing
10

Example 5

[root@localhost wrapper]# vi autoboxing.java
import java.lang.Long;

class autoboxing
{
public static void main(String args[])
{
long a = 10;
Long i = new Long(a);
System.out.println(i);
}
}

Compile Your Program

[root@localhost wrapper]# javac autoboxing.java

Output

[root@localhost wrapper]# java autoboxing
10

Example 6

[root@localhost wrapper]# vi autoboxing.java
import java.lang.Double;

class autoboxing
{
public static void main(String args[])
{
double a = 10.35;
Double i = new Double(a);
System.out.println(i);
}
}

Compile Your Program

[root@localhost wrapper]# javac autoboxing.java

Output

[root@localhost wrapper]# java autoboxing
10.35

Example 7

[root@localhost wrapper]# vi autoboxing.java
import java.lang.Float;

class auto
{
public static void main(String args[])
{
float a = 5.6;
Float i = new Float(a);
System.out.println(i);
}
}

Compile Your Program

[root@localhost wrapper]# javac autoboxing.java

Output

[root@localhost wrapper]# java autoboxing
5.6

Example 8

[root@localhost wrapper]# vi autoboxing.java
import java.lang.Boolean;

class auto
{
public static void main(String args[])
{
boolean a = true;
Boolean i = new Boolean(a);
System.out.println(i);
}
}

Compile Your Program

[root@localhost wrapper]# javac autoboxing.java

Output

[root@localhost wrapper]# java autoboxing
true

Unboxing

It is a mechanism to convert Object to Primitive or in other words, an unboxing conversion would take place when someone tries to assign the wrapper instance to a primitive variable. This can be understand with the help of various examples as explained below.

Example 1

[root@localhost wrapper]# vi unboxing.java
import java.lang.Integer;

class unboxing
{
public static void main(String args[])
{
Integer a = new Integer(10);
int b = a;
System.out.println(b);
}
}

Compile Your Program

[root@localhost wrapper]# javac unboxing.java

Run Your Program

[root@localhost wrapper]# java unboxing
10

Example 2

[root@localhost wrapper]# vi unboxing.java
import java.lang.Boolean;

class unboxing
{
public static void main(String args[])
{
Boolean a = new Boolean(true);
boolean b = a;
System.out.println(b);
}
}

Compile Your Program

[root@localhost wrapper]# javac unboxing.java

Run Your Program

[root@localhost wrapper]# java unboxing
true

Example 3

[root@localhost wrapper]# vi unboxing.java
import java.lang.Byte;

class unboxing
{
public static void main(String args[])
{
Byte a = new Byte("10");
byte b = a;
System.out.println(b);
}
}

Compile Your Program

[root@localhost wrapper]# javac unboxing.java

Run Your Program

[root@localhost wrapper]# java unboxing
10

Example 4

[root@localhost wrapper]# vi unboxing.java
import java.lang.Character;

class unboxing
{
public static void main(String args[])
{
Character a = new Character('c');
char b = a;
System.out.println(b);
}
}

Compile Your Program

[root@localhost wrapper]# javac unboxing.java

Run Your Program

[root@localhost wrapper]# java unboxing
c

Example 5

[root@localhost wrapper]# vi unboxing.java
import java.lang.Long;

class unboxing
{
public static void main(String args[])
{
Long a = new Long(10);
long b = a;
System.out.println(b);
}
}

Compile Your Program

[root@localhost wrapper]# javac unboxing.java

Run Your Program

[root@localhost wrapper]# java unboxing
10

Example 6

[root@localhost wrapper]# vi unboxing.java
import java.lang.Float;

class unboxing
{
public static void main(String args[])
{
Float a = new Float(10.35);
Float b = a;
System.out.println(b);
}
}

Compile Your Program

[root@localhost wrapper]# javac unboxing.java

Run Your Program

[root@localhost wrapper]# java unboxing
10.35

Example 7

[root@localhost wrapper]# vi unboxing.java
import java.lang.Double;

class unboxing
{
public static void main(String args[])
{
Double a = new Double(10.35);
double b = a;
System.out.println(b);
}
}

Compile Your Program

[root@localhost wrapper]# javac unboxing.java

Run Your Program

[root@localhost wrapper]# java unboxing
10.35

Example 8

[root@localhost wrapper]# vi unboxing.java
import java.lang.Short;

class unboxing
{
public static void main(String args[])
{
Short a = new Short("10");
short b = a;
System.out.println(b);
}
}

Compile Your Program

[root@localhost wrapper]# javac unboxing.java

Run Your Program

[root@localhost wrapper]# java unboxing
10

 

 

 

Popular Recommendations:-

Solved: nrpe.service: main process exited, code=exited, status=2/INVALIDARGUMENT

C# data types with Best Examples (.NET v4.7)

How to Transfer Files to an AWS EC2 Instance Using WinSCP in 3 Easy Steps

Learn HTML Image Maps(v5) with Best Examples

Learn HTML Tables(v5) with Best Examples

How to Install PHP on RedHat/CentOS 7 with Easy Steps

How to Install Ruby on Ubuntu 18.04 with Easy Steps

Easy Steps to Install GCC(C and C++ Compiler) on CentOS 7

Leave a Comment