What it is?
Even though the name includes boxing, this is all about wrapping and unwrapping primitive types with its corresponding wrapper class.
Even though the name includes boxing, this is all about wrapping and unwrapping primitive types with its corresponding wrapper class.
No Idea…
Ok, Following example will explain all.
Ok, Following example will explain all.
// Java program to illustrate the concept
// of Autoboxing and Unboxing
import java.io.*;
class Independor
{
public static void main (String[] args)
{
// creating an Integer Object (wrapper class of int)
// with value 10.
Integer i = new Integer(10);
// unboxing the Object
int i1 = i;
System.out.println("Value of i: " + i);
System.out.println("Value of i1: " + i1);
//Autoboxing of char
Character gfg = 'a';
// Auto-unboxing of Character (wrapper class of char)
char ch = gfg;
System.out.println("Value of ch: " + ch);
System.out.println("Value of gfg: " + gfg);
}
}
Following are the primitive types and its corresponding wrapper classes.
Why Autoboxing/Unboxing?
- Autoboxing and unboxing lets developers write cleaner code, making it easier to read.
- The technique let us use primitive types and Wrapper class objects interchangeably and we do not need to perform any typecasting explicitly.
That’s it… You got the idea right? It’s not a complicate subject to understand, but if you have any questions let me know below. Cheers!