03: ♥♦ Java autoboxing & unboxing benefits & caveats interview Q&As

Q1. What do you understand by the terms “autoboxing” and “autounboxing” in Java?
A1. Java automatically converts a primitive type like “int” into corresponding wrapper object class Integer. This is known as the autoboxing. When it converts a wrapper object class Integer back to its primitive type “int”, it is know as “autounboxing“.

Example 1:

This can be applied to one of 8 primitivies in Java to convert from primitive to wrapper via autoboxing and from wrapper to primitive via autounboxing. Autoboxing and unboxing can happen anywhere where an object is expected and primitive type is available

Example 2:

Q2. What are the benefits of autoboxing?
A2. Less code to write, and the code looks cleaner.

For example, you don’t have to do as shown below:

More readable with autoboxing

Q3. What are some of the pitfalls of autoboxing?
A3. It is very convenient to havae autoboxing, but it can cause issues and many beginners fall into it caveats.

1. Unnecessary Object creation due to Autoboxing

Q. How do you know unnecessary objects are being created?
A. jmap to the rescue.

Step 1: Run the above code.

Step 2: Open a DOS or Unix command prompt and run the following commands. “jps” to find the process id, and then “jmap” to print the object graph

Step 3: Inspect the mem.txt file

after some time

You can see the growing instances and bytes.

Now try the samething after fixing the code as shown below.

after some time

The improved code does not create unnecessary Integer objects. You may also like the detailed “javap, jps, jmap, and jvisualvm tutorial – analyzing the heap histogram

2. GC overhead

Unnecessarily creating too many objects and then discarding them will increase the Garbage Collection overhead. This may cause performance impact due to more frequent garbage collection.

3. java.lang.NullPointerException

Especially when mixing object and primitive in equality and relational operator.

Conditional operators can cause NullPointerException.

Since d1 is primitive, d2 is implicitly tried to auto unbox. To fix it, you need to change d1 to wrapper object type “Double“. This way auto unboxing won’t take place.

4. Overloading

Q. What will be the output of the following code?

A. The result is 1, because there is no direct conversion from Integer to Long, so the “conversion” from Integer to long is used.

Q4. How will you go about debugging auto boxing and unboxing error?

1) Being aware of the potential auto boxing and unboxing caveats discussed above.

2) Configuring your IDE to pick up auto boxing and unboxing error. For example, in eclipse

Arulkumaran Kumaraswamipillai

Mechanical Engineer to freelance Java developer within 3 years. Freelancing since 2003 for the major banks, telecoms, retail & government organizations. Attended 150+ Java job interviews, and most often got 3-6 job offers to choose from. Published Java/JEE books via Amazon.com in 2005, and sold 35K+ copies. Books are outdated and replaced with this online Java training. join my LinkedIn group.

Posted in Data types, FAQs Free, Understanding Core Java
Tags: ,

800+ Java Q&As – ♥Free | ♦FAQs

open all | close all

100+ Java Tutorials – Step by step

open all | close all

How good are your

open all | close all