Home | | Web Programming | Optional, OptionalDouble, OptionalInt, and OptionalLong - Java

Chapter: Java The Complete Reference : The Java Library : java.util : More Utility Classes

Optional, OptionalDouble, OptionalInt, and OptionalLong - Java

JDK 8 adds classes called Optional, OptionalDouble, OptionalInt, and OptionalLong that offer a way to handle situations in which a value may or may not be present.

Optional, OptionalDouble, OptionalInt, and OptionalLong

 

JDK 8 adds classes called Optional, OptionalDouble, OptionalInt, and OptionalLong that offer a way to handle situations in which a value may or may not be present. In the past, you would normally use the value null to indicate that no value is present. However, this can lead to null pointer exceptions if an attempt is made to dereference a null reference. As a result, frequent checks for a null value were necessary to avoid generating an exception. These classes provide a better way to handle such situations.

The first and most general of these classes is Optional. For this reason, it is the primary focus of this discussion. It is shown here:

 

class Optional<T>

 

Here, T specifies the type of value stored. It is important to understand that an Optional instance can either contain a value of type T or be empty. In other words, an Optional object does not necessarily contain a value. Optional does not define any constructors, but it does define several methods that let you work with Optional objects. For example, you can determine if a value is present, obtain the value if it is present, obtain a default value when no value is present, and construct an Optional value. The Optional methods are shown in Table 19-3.




The best way to understand Optional is to work through an example that uses its core methods. At the foundation of Optional are isPresent( ) and get( ). You can determine if a value is present by calling isPresent( ). If a value is available, it will return true. Otherwise, false is returned. If a value is present in an Optional instance, you can obtain it by calling get( ). However, if you call get( ) on an object that does not contain a value, NoSuchElementException is thrown. For this reason, you should always first confirm that a value is present before calling get( ) on an Optional object.

 

Of course, having to call two methods to retrieve a value adds overhead to each access. Fortunately, Optional defines methods that combine the check for a value with the retrieval of the value. One such method is orElse( ). If the object on which it is called contains a value, the value is returned. Otherwise, a default value is returned.

 

Optional does not define any constructors. Instead, you will use one of its methods to create an instance. For example, you can create an Optional instance with a specified value by using of( ). You can create an instance of Optional that does not contain a value by using empty( ).

 

The following program demonstrates these methods:

 

// Demonstrate several Optional<T> methods

 

import java.util.*;

 

class OptionalDemo {

 

public static void main(String args[]) {

 

Optional<String> noVal = Optional.empty();

 

Optional<String> hasVal = Optional.of("ABCDEFG");

 

if(noVal.isPresent()) System.out.println("This won't be displayed"); else System.out.println("noVal has no value");

 

if(hasVal.isPresent()) System.out.println("The string in hasVal is: " + hasVal.get());

 

String defStr = noVal.orElse("Default String"); System.out.println(defStr);

 

}

 

}

 

The output is shown here:

 

noVal has no value

 

The string in hasVal is: ABCDEFG Default String

 

As the output shows, a value can be obtained from an Optional object only if one is present. This basic mechanism enables Optional to prevent null pointer exceptions.

The OptionalDouble, OptionalInt, and OptionalLong classes work much like Optional, except that they are designed expressly for use on double, int, and long values, respectively. As such, they specify the methods getAsDouble( ), getAsInt( ), and getAsLong( ), respectively, rather than get( ). Also, they do not support the filter( ), ofNullable( )map( ) and flatMap( ) methods.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : The Java Library : java.util : More Utility Classes : Optional, OptionalDouble, OptionalInt, and OptionalLong - Java |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.