DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Database Integration Tests With Spring Boot and Testcontainers
  • Prompt Engineering: Unlocking the Power of Generative AI Models
  • Implementing RBAC in Quarkus
  • Build a Web3 Ticketing System and Disrupt Online Ticketing

Trending

  • Database Integration Tests With Spring Boot and Testcontainers
  • Prompt Engineering: Unlocking the Power of Generative AI Models
  • Implementing RBAC in Quarkus
  • Build a Web3 Ticketing System and Disrupt Online Ticketing
  1. DZone
  2. Coding
  3. Java
  4. JDK 8 Versus JDK 10: A Ternary, Unboxing Difference

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!

Dustin Marx user avatar by
Dustin Marx
·
Jun. 14, 18 · Presentation
Like (9)
Save
Tweet
Share
26.48K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Java (programming language) Java Development Kit

Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Database Integration Tests With Spring Boot and Testcontainers
  • Prompt Engineering: Unlocking the Power of Generative AI Models
  • Implementing RBAC in Quarkus
  • Build a Web3 Ticketing System and Disrupt Online Ticketing

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: