+ 14

What is Boxing, Unboxing and Autoboxing in Java?

What is Boxing, Unboxing and Autoboxing in Java?

12th Mar 2019, 4:50 AM
Khalid Saifullah Fuad
Khalid Saifullah Fuad - avatar
3 ответов
+ 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); } }
12th Mar 2019, 5:10 PM
Arjun
Arjun - avatar
+ 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.
12th Mar 2019, 6:32 AM
M_N
+ 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);
12th Mar 2019, 7:34 PM
M_N