What are Boxing, Autoboxing and Auto-Unboxing in java example
Boxing :- Convert primitive data to its wrapper class object.
Example :- char to Charactor
int to Integer
boolean to Boolean
float to Float
double to Double;
Unboxing :- Java primitive type to its wrappers class:
Example :- Boolean to boolean
Byte to byte,
Character to char,
Double to double,
Float to float,
Long to long,
Integer to int,
public abstract class Test {
public static void main(String[] args) throws Exception {
boolean b = true;
//Explicit Boxing
Boolean b1 = new Boolean(b);
//Explicit Unboxing
boolean b2 = b1.booleanValue();
// Automatic Boxing
b1 = b;
//Automatic Unboxing
b2 = b1;
int i = 50;
// Explicit Boxing
Integer i1 = new Integer(i);
// Explicit Unboxing
int j = i1.intValue();
// Automatic Boxing
i1 = i;
// Automatic Unboxing
j = i1;
}
}
Example :- char to Charactor
int to Integer
boolean to Boolean
float to Float
double to Double;
Unboxing :- Java primitive type to its wrappers class:
Example :- Boolean to boolean
Byte to byte,
Character to char,
Double to double,
Float to float,
Long to long,
Integer to int,
public abstract class Test {
public static void main(String[] args) throws Exception {
boolean b = true;
//Explicit Boxing
Boolean b1 = new Boolean(b);
//Explicit Unboxing
boolean b2 = b1.booleanValue();
// Automatic Boxing
b1 = b;
//Automatic Unboxing
b2 = b1;
int i = 50;
// Explicit Boxing
Integer i1 = new Integer(i);
// Explicit Unboxing
int j = i1.intValue();
// Automatic Boxing
i1 = i;
// Automatic Unboxing
j = i1;
}
}
Comments
Post a Comment