XRB’s Blog
Published in

XRB’s Blog

Java Quiz of the Day

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.




Random thoughts of a Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Patrick Jayet

Patrick Jayet

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

More from Medium

SAP Commerce: Overriding UserPriceGroup in session

Let’s talk about head first Java

A Quick walkthrough: JVM

Overriding .equals method for inheriting classes in Java