Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

JDK 8 Versus JDK 10: A Ternary, Unboxing Difference

DZone's Guide to

JDK 8 Versus JDK 10: A Ternary, Unboxing Difference

A subtle but important difference comes with JDK 10 when it comes to ternary statements and unboxing. Read on for the details!

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

A recent Nicolai Parlog (@nipafx) tweet caught my attention because it referenced an interesting StackOverflow discussion about a changed behavior between JDK 8 and JDK 10 and I asked: "Why?" The issue cited on the StackOverflow thread by SerCe ultimately came down to the implementation being changed between JDK 8 and JDK 10 to correctly implement the Java Language Specification.

The following code listing is (very slightly) adapted from the original example provided by SerCe on the StackOverflow thread.

public static void demoSerCeExample()  
{  
   try  
   {  
      final Double doubleValue = false ? 1.0 : new HashMap<String, Double>().get("1");  
      out.println("Double Value: " + doubleValue);  
   }  
   catch (Exception exception)  
   {  
      out.println("ERROR in 'demoSerCeExample': " + exception);  
   }  
}  


When the above code is compiled and executed with JDK 8, it generates an output like this: Double Value: null.

When the above code is compiled and executed with JDK 10, it generates an output like this: ERROR in 'demoSerCeExample': java.lang.NullPointerException.

In JDK 8, the ternary operator returned null for its assignment to the local variable,doubleValue , but in JDK 10, a NullPointerException is thrown instead for the same ternary statement.

Two tweaks to this example lead to some interesting observations. First, if the literal constant 1.0 expressed in the ternary operator is specified instead as Double.valueOf(1.0), both JDK 8 and JDK 10 set the local variable to null rather than throwing a NullPointerException. Second, if the local variable is declared with a primitive type double instead of reference type Double, the NullPointerException is always thrown, regardless of the Java version and regardless of whether Double.valueOf(double) is used. This second observation makes sense, of course, because no matter how the object or reference is handled by the ternary operator, it must be dereferenced at some point to be assigned to the primitive double type and that will always result in a NullPointerException in the example.

The following table summarizes these observations:

Complete Ternary Statement Setting of Local Variable doubleValue
JDK 8 JDK 10
Double doubleValue
   =  false
    ? 1.0
    : new HashMap<String, Double>().get("1");

null NullPointerException
double doubleValue
   =  false
    ? 1.0
    : new HashMap<String, Double>().get("1");

NullPointerException NullPointerException
Double doubleValue
   =  false
    ? Double.valueOf(1.0)
    : new HashMap<String, Double>().get("1");

null null
double doubleValue
   =  false
    ? Double.valueOf(1.0)
    : new HashMap<String, Double>().get("1");

NullPointerException NullPointerException


For this general ternaryy example,  the only approach that avoids NullPointerException in both versions of Java is the version that declares the local variable as a reference type Double (no unboxing is forced) and uses Double.valueOf(double) so that the reference Double is used throughout the ternary rather than primitive double. If the primitive double is implied by specifying only 1.0, then the Double returned by the Java Map is implicitly unboxed (dereferenced) in JDK 10 and that leads to the exception. According to Brian Goetz, JDK 10 brings the implementation back into compliance with the specification.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java ,jdk 8 ,jdk 10 ,ternary ,unboxing ,difference ,versions

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}