01: ♥ What is wrong with this code? Auto-unboxing, switch, short circuit & exception

Auto-unboxing, switch, short circuit logical operators & exception handling are core Java basics that sometimes lead to obscure errors if not understood well & often tested in coding tests.

Q1. What is wrong with this code?

A1. compile-error at switch(i) as switch works only with “int” and “String” (i.e. from Java 7 onwards) data types.

Q2. What is wrong with this code?

A2. “java.lang.NullPointerException” at “entry.value > 0”. Use short circuit “&&” instead to not check “entry.value > 0” when “entry == null”.

Q3. What is wrong with this code?

A3. The output will be

This is because of the hidden danger of autoboxing.

1) map.put(new Integer(1), “Hello World”); // auto boxed
2) map.get(new Byt(1)); // null

Integer(1) != Byte(1).

Q4. What is wrong with this code?

A4. java.lang.NullPointerException due to autounboxing. The two expressions around “:” must return the same type. This means Java tries to convert the expression d2 to primitive “double” value. This means the call “doubleValue()” on d2, which is null will throw a “java.lang.NullPointerException”

Q5. What is wrong with this code?

A5. Compile-time error. Exceptions are polymorphic in nature and more specific exceptions need to be caught before the generic exceptions. So, “IOException” must be caught before “Exception”.

share...Share on LinkedIn5Share on Google+0Share on Facebook0
The following two tabs change content below.
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.
What An Excellent Book!

I have got a copy of your book last week. It is really an incredible book that I have ever seen. I have seen so many Java books like Deitel, Ivor Horton. They are also good. But the book of yours can give me an overall bird-eye view of the Java Environment together with so many useful questions. I am totally impressed with the way you have learned Java.

Yours Sincerely
Ye Tun Oo ( More )

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 What is wrong with this code?

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

open all | close all

Pressed for time? 200+ Quick Prep

open all | close all

16 Technical Key Areas to be a top-notch

open all | close all

100+ Java Tutorials – Step by step

open all | close all

100+ Java Coding Exercises

open all | close all

How good are your

open all | close all