Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Boxing

The process of converting primitive type variable into its equivalent object type is known as boxing.
That is the process of inserting primitive type variable into its equivalent wrapper is known as boxing.

Java Programming Language Primitive type to equivalent Object type

Boxing is also known as wrapping.
Boxing is done with the help of classes known as wrappers.
Wrappers in Java are Short, Byte, Character, Boolean, Integer, Float, Double & Long.

Why Boxing?

Generally, for simple calculations, we use primitive types like int, char, double, etc. because they are very simple to use as compared to object types.
But sometimes we may need to operate by using objects only because some data structures of Java operate only with objects.
Therefore it becomes impossible to use primitive types while working with those data structures & may lead to a problem.
To avoid this problem, Java introduced type wrappers which help us to use primitive types as objects.
Wrappers are some classes that encapsulate a simple primitive type to form an object.

Java Programming Language boxing in Auto Boxing

Un-Boxing

The process of extracting a primitive type from wrapper is known as un-boxing.
That is processing of converting object to primitive type back again is known as un-boxing.

Java Programming Language Un-Boxing in Auto Un-Boxing

The Integer Wrapper/Class

The Integer is a wrapper used to encapsulate an int primitive type within it.
If primitive type int value is encapsulated within an Integer wrapper, then we can use any int value as an object of Integer type.

Java Programming Language Integer Wrapper or Class in Auto-Boxing and Un-Boxing

Example of boxing

Integer i = new Integer(10); // boxing

To extract int value from Integer wrapper, we should use intValue() method.

Syntax of intValue() method

int intValue()

Example of unboxing

 int val = i.intValue(); // unboxing

This technique of obtaining a value from an object is known as unboxing.

Demonstration

 class ManualWrap 
 {
     public static void main(String args[])
     {
         Integer i = new Integer(10);  // boxing
         int val = i.intValue();       // unboxing
         System.out.println("val = "+val);
     }
 }

Output

val = 10

The Character Wrapper

The Character is a wrapper used to encapsulate a char primitive type within it.
If primitive type char value is used with a character wrapper, then we can use any char value as an object of character type.

Java Programming Language Character Wrapper or Class in Auto-Boxing and Un-Boxing

Example of boxing

Character C = new Character('A');        // boxing

To extract char value from Character object we should use charValue() method.

Syntax of charValue() method

char charValue()

Example of unboxing

 char ch = C.charValue();       // unboxing

Demonstration

 class ManualWrap2 
 {
     public static void main(String args[])
     {
         Character C = new Character('A');  // boxing
         char ch = C.charValue();           // unboxing
         System.out.println("ch = "+ch);
     }
 }

Output

ch = A

The Boolean Wrapper

The Boolean is a wrapper used to encapsulate boolean values true or false within it.

Java Programming Language Boolean Wrapper or Class in Auto-Boxing and Un-Boxing

Example of boxing

Boolean B1 = new Boolean (true);    // boxing

To extract boolean value from Boolean object we should use booleanValue() method.

Syntax of booleanValue() method

char booleanValue()

Example of unboxing

 boolean b1 = B1.booleanValue();      // unboxing

Demonstration

 class ManualBoolWrap 
 {
     public static void main(String args[]) 
     {
         Boolean B1 = new Boolean(true);
         Boolean B2 = new Boolean("false");
         boolean b1 = B1.booleanValue();
         boolean b2 = B2.booleanValue();
         System.out.println("b1 = "+b1);
         System.out.println("b2 = "+b2);
     }
 }

Output

b1 = true
b2 = false

Similarly, all wrappers of Java are used to encapsulate a primitive type into its equivalent type wrapper.
All wrappers override toString() method.

 

Auto-boxing

Auto-boxing is a technique that helps to insert primitive type within its equivalent wrapper automatically.

 

Auto-Unboxing

Auto-Unboxing is a technique that helps to obtain primitive type from its wrapper automatically.

There is no need of using methods like intValue(), charValue(), floatValue(), etc. to obtain primitive type from its wrapper.

Difference between Auto-Boxing & Manually-Boxing of primitive types

Integer i = new Integer(10);    // Manually boxing
Integer i = 10;    // Auto boxing

Difference between Auto-UnBoxing & Manually-UnBoxing of primitive types

 int val = i.intValue();    // Manually Unboxing
int val = i;    // Auto Unboxing

Example – I

 class AutoBoxing1 
 {
     public static void main(String args[])
     {
         Integer i = 10; 
         int val = i;    
         System.out.println("integer val = "+val);
        
         Character c = 'Z'; 
         char ch = c;        
         System.out.println("character val :: "+ch);
     }
 }

Output

integer val = 10
character val :: Z

Example – II

 class AutoBoxing2 
 {
     public static void demo(Integer i1, Integer i2) // Auto boxing
     {
         System.out.println("addition of two Integers = "+(i1+i2)); // Auto Unboxing
     }
    
     public static void main(String args[])
     {
         demo(20,30);
     }
 }

Output

addition of two Integers = 50

 

Leave a reply