Auto boxing & unboxing
// Contributed by - Anuj Das ( GC College, Silchar - @ Department of Computer Science )
public class AutoboxingUnboxing {
public static void main(String[ ] args) {
// AUTOBOXING - Converting a primitive value into an object of the corresponding wrapper class
byte a=10;
Byte b = Byte.valueOf(a); // Boxing using valueOf() static method
System.out.println("After Autoboxing: " + b);
int x=100;
Integer y = Integer.valueOf(x); // Boxing using valueOf() static method
System.out.println("After Autoboxing: " + y);
// UNBOXING - Converting an object of a wrapper type to its corresponding primitive value
int z = y; // Unboxing
System.out.println("After Unboxing: " + z);
}
}
OUTPUT
cmd-> javac AutoboxingUnboxing.java
cmd-> java AutoboxingUnboxing
After Autoboxing: 10
After Autoboxing: 100
After Unboxing: 100