How to Prevent null Checks Using Optional Type in Java 8

  • google plus

In Brief...

Avoiding the null pointer exception has been a long-standing goal of Java developers. The standard technique to avoid these exceptions is to check an object to find out if it has a null value. With Java 8, a more elegant technique using the Optional class is available that grants Java 8 a feature that has been available in other languages for years. The Optional class contains methods such as ofNullable and ifPresent that enable the developer to be proactive in handling variables that potentially have null values. Learn how to prevent null checks using the Optional type by following these four steps.

Take our Introduction to Java Training course for free.

See the Course Outline and Register


  1. Open your text editor and create the Java program that will demonstrate the use of Optional. Type in the following Java statements:
    import java.util.*;	
    public class PreventnullChecksUsingOptionalType {
    	public static String testString=null;
    	public static Optional optionalTestString=Optional.ofNullable(null);
    	public static void main (String[] args) {
    		if (testString==null) {
    			System.out.println("From traditional null check: String is null!");
    		else {
    			System.out.format("String is %s \n", testString);
    		if (optionalTestString.isPresent()) {
    			System.out.format("String is %s \n", testString);
    		else {
    			System.out.println("From Optional class: String is null!");

    The program begins by using the traditional technique of checking for null values. Next, the program demonstrates the use of an Optional variable. Note the use of ofNullable to proactively define a variable with a null value. In this way, the developer is clearly indicating his or her intention of defining a variable with a null value. When the variable is referenced in the code, the ifPresent method is used instead of an explicit null check. This approach is clear and self-documenting.

  2. Save your file as
  3. Open a command prompt and navigate to the directory containing your new Java program. Then type in the command to compile the source and hit Enter.
    Compile Program with Optional Type
  4. You are ready to test your Java program. Type in the command to run the Java runtime launcher and hit Enter. Notice the output shows the results of using the traditional null check and the new Optional class.
    Run Program with Optional Type

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, XML, Windows, Java, Adobe, HTML5, JavaScript, Angular, and much more. Check out our complete course catalog.



Author: Stephen Withrow

Stephen has over 30 years' experience in training, development, and consulting in a variety of technology areas including Java, C, C++, XML, JavaScript, AJAX, Tomcat, JBoss, Oracle, and DB2. His background includes design and implementation of business solutions on client/server, Web, and enterprise platforms. Stephen is a published writer in both technical and non-technical endeavors. Stephen received an undergraduate degree in Computer Science and Physics from Florida State University.