Using Library Classes — Question 15
Back to all questionsQuestion 15
In the following code, identify the statement where autoboxing is taking place :
Integer i = new Integer(10);
if (i < 100)
System.out.println(i);
else
System.out.println(i + 10);Autoboxing is not taking place in this code as in the statement Integer i = new Integer(10);, we are explicitly making an Integer object.
if(i < 100)
Auto-unboxing is taking place in this statement, as the value of an object cannot be compared to a numeric value 100 directly but a primitive integer type value can be compared. So, unboxing takes place before the value of i gets compared to 100.
System.out.println(i);
Auto-unboxing is happening in this statement as directly an object cannot be printed but a primitive value can be printed. So, unboxing occurred before the value of i gets printed.
System.out.println(i + 10);
Auto-unboxing is taking place in this statement, as the value of an object cannot be used with + operator like a primitive integer type value. Thus, i gets auto-unboxed to a primitive type value and the value of i gets modified by the + operator.