+ 14
What is Boxing, Unboxing and Autoboxing in Java?
What is Boxing, Unboxing and Autoboxing in Java?
3 Answers
+ 6
Boxing
Converting primitive datatype to object is called boxing.
Example :
Integer obj = new Integer ("2526");
Autoboxing
Converting a primitive value into an object of the corresponding wrapper class is called autoboxing.
Example :
Character gfg = 'a';
Unboxing
Converting an object into corresponding primitive datatype is known as unboxing.
Example :
public class Sample {
public static void main (String args[]){
Integer obj = new Integer("2526");
int i = obj.intValue();
System.out.println(i);
}
}
+ 2
Integer a = new Integer(7);
int b = 8;
System.out.print(a < b);
Integer is an Object, int is a primitive type. And when we compare them JVM knows when to use Boxing.
+ 1
Arjun, since JDK 5 you don't need to use .intValue():
Integer obj = new Integer("2526");
int i = obj;
Or just:
int i = new Integer(2526);