Java Quiz of the Day

Patrick Jayet
XRB’s Blog
Published in
1 min readMay 19, 2010

What gives

Double a = null;
Double b = a == null ? a : 1.0;

Response: as strange as it can seem at first look, b is not equal to null after the second statement, but the code produces a NullPointerException.

The reason for that is the type of the second expression (double) inside the ternery operator. The first expression ‘a’ of type Double is unboxed to double (calling doubleValue() on ‘a’ which is null), causing the NPE.

You can find a discussion about that here.

--

--

Patrick Jayet
XRB’s Blog

Polyglot (FR, DE, EN, ES), polyglot programmer (Java, Groovy, Ruby, Swift, Objective-C, Scala, Python, O’Caml) polyglot methodologist (Scrum, Kanban, Lean).