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

Posted by Tori Wieldt on Oracle Blogs See other posts from Oracle Blogs or by Tori Wieldt
Published on Tue, 3 Jun 2014 17:17:56 +0000 Indexed on 2014/06/03 21:33 UTC
Read the original article Hit count: 216

Filed under:

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!"

© Oracle Blogs or respective owner

Related posts about /Oracle/General