Tech Article: Tired of Null Pointer Exceptions? Use Java SE 8's Optional!

A wise man once said you are not a real Java programmer until you've dealt with a null pointer exception. The null reference is the source of many problems because it is often used to denote the absence of a value. Java SE 8 introduces a new class called java.util.Optional that can alleviate some of these problems. In the tech article "Tired of Null Pointer Exceptions? Use Java SE 8's Optional!" Java expert Raoul-Gabriel Urma shows you how to make your code more readable and protect it against null pointer exceptions.

Urma explains "The purpose of Optional is not to replace every single null reference in your codebase but rather to help design better APIs in which—just by reading the signature of a method—users can tell whether to expect an optional value. In addition, Optional forces you to actively unwrap an Optional to deal with the absence of a value; as a result, you protect your code against unintended null pointer exceptions."

Learn how to go from writing painful nested null checks to writing declarative code that is composable, readable, and better protected from null pointer exceptions. Read "Tired of Null Pointer Exceptions? Use Java SE 8's Optional!"

Comments:

To summarize, the article opens with the elegant solution in Groovy:
String version = computer?.getSoundcard()?.getUSB()?.getVersion();

and then introduces the "equivalent" elegant solution in Java 8:
String name = computer.flatMap(Computer::getSoundcard)
.flatMap(Soundcard::getUSB)
.map(USB::getVersion)
.orElse("UNKNOWN");

And of course, for all this "simplicity" to work, you have to modify both Computer and Soundcard classes.

Sorry Java, you're not even close this time.

Posted by alex on June 04, 2014 at 06:30 AM PDT #

From the author (Raoul-Gabriel Urma):

The comment essentially argues that the Java solution is not as syntactically pleasant as to what’s possible in Groovy. A lot of features in Java are more verbose compared to features in other programming languages (historical and compatibility reasons). However, that’s not the point of the article.

The article shows how by adopting the Optional feature you can reduce null pointer exceptions compared to what was previously possible *in Java* using nested null checks.

In addition, the use of Optional is as I conclude the article "to help design better APIs in which—just by reading the signature of a method—users can tell whether to expect an optional value”. By using the operator “?.” in Groovy you don’t get that. You still need to remember or decide to use the operator. In contrast, with Optional you are *forced* to deal with the possibility of a missing value explicitly.
___________________________________________________________

You can contact Raoul directly at http://www.urma.com/

Posted by Tori on June 04, 2014 at 10:01 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Insider News from the Java Team at Oracle!

duke
Links


Search

Archives
« September 2015
SunMonTueWedThuFriSat
  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today