Home » Java » Core Java » Java Auto-Unboxing Gotcha. Beware!

About Lukas Eder

Lukas Eder
Lukas is a Java and SQL enthusiast developer. He created the Data Geekery GmbH. He is the creator of jOOQ, a comprehensive SQL library for Java, and he is blogging mostly about these three topics: Java, SQL and jOOQ.

Java Auto-Unboxing Gotcha. Beware!

What do you think that the following code snippet will print?

Object o = true ? new Integer(1) : new Double(2.0);

Yes! It will print:


What? 1.0? But I have assigned an Integer to my o variable. Why does it print 1.0? It turns out that there is a subtle little specification section in the JLS’s §15.25, which specifies the ternary operator. Here’s what is applied to the above:

The type of a conditional expression is determined as follows:

  • […]
  • Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases:
    • […]
    • Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted type of the second and third operands.
      Note that binary numeric promotion performs value set conversion (§5.1.13) and may perform unboxing conversion (§5.1.8).

Binary numeric promotion may implicitly perform unboxing conversion! Eek! Who would have expected this? You can get a NullPointerException from auto-unboxing, if one of the operands is null, the following will fail

Integer i = new Integer(1);
if (i.equals(1))
    i = null;
Double d = new Double(2.0);
Object o = true ? i : d; // NullPointerException!

Obviously (obviously !?) you can circumvent this problem by casting numeric types to non-numeric types, e.g. Object

Object o1 = true 
  ? (Object) new Integer(1) 
  : new Double(2.0);

The above will now print


Credits for discovery of this gotcha go to Paul Miner, who has explained this more in detail here on reddit.

Reference: Java Auto-Unboxing Gotcha. Beware! from our JCG partner Lukas Eder at the JAVA, SQL, AND JOOQ blog.
(0 rating, 0 votes)
You need to be a registered member to rate this.
4 Comments Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
Email address:

Leave a Reply

4 Comments on "Java Auto-Unboxing Gotcha. Beware!"

newest oldest most voted
Notify of

in fact the point there is more about the ternary operator that is a bit weird in some cases

notice that the some code especially the last sample might not gives the same result using groovy and using java

Yannick Majoros

Right, nothing to do with unboxing. This will print the same result without any unboxing:

System.out.println(true ? 1 : 2.0);

Recommanding to cast to Object is a bad idea.

So is this in a post-java 1.4 world:

Integer i = new Integer(1);

Should simply be:

Integer i = 1;

Witek Baranowski

It’s not new and for sure Paul Miner didnt discover it :)

Link to Polish java beginner tutorial from 2010…


Lukas Eder

Yes, claiming that it was new to everyone was a bit premature. This particular case is probably as old as unboxing itself. For example, it made it into the NASA coding standards as R40: