The Wayback Machine - https://web.archive.org/web/20240712034537/https://www.geeksforgeeks.org/java-8-optional-class/
Open In App

Java 8 Optional Class

Last Updated : 24 Jun, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Every Java Programmer is familiar with NullPointerException. It can crash your code. And it is very hard to avoid it without using too many null checks. So, to overcome this, Java 8 has introduced a new class Optional in java.util package. It can help in writing a neat code without using too many null checks. By using Optional, we can specify alternate values to return or alternate code to run. This makes the code more readable because the facts which were hidden are now visible to the developer.

Java
// Java program without Optional Class

public class OptionalDemo {
    public static void main(String[] args)
    {
        String[] words = new String[10];
        String word = words[5].toLowerCase();
        System.out.print(word);
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException

To avoid abnormal termination, we use the Optional class. In the following example, we are using Optional. So, our program can execute without crashing.

The above program using Optional Class

Java
// Java program with Optional Class

import java.util.Optional;

// Driver Class
public class OptionalDemo {
      // Main Method
    public static void main(String[] args)
    {
        String[] words = new String[10];
        
          Optional<String> checkNull = Optional.ofNullable(words[5]);
        
          if (checkNull.isPresent()) {
            String word = words[5].toLowerCase();
            System.out.print(word);
        }
        else
            System.out.println("word is null");
    }
}

Output
word is null

Optional is a container object which may or may not contain a non-null value. You must import java.util package to use this class. If a value is present, isPresent() will return true and get() will return the value. Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() which returns a default value if the value is not present, and ifPresent() which executes a block of code if the value is present. This is a value-based class, i.e their instances are : 

  • Final and immutable (though may contain references to mutable objects).
  • Considered equal solely based on equals(), not based on reference equality(==).
  • Do not have accessible constructors.

Static Methods: Static methods are the methods in Java that can be called without creating an object of the class. They are referenced by the class name itself or reference to the object of that class. 

Syntax : 

public static void geek(String name)
{
// code to be executed....
}

// Must have static modifier in their declaration.
// Return type can be int, float, String or user-defined data type.

Important Points: Since Static methods belong to the class, they can be called to without creating the object of the class. Below given are some important points regarding Static Methods : 

  • Static method(s) are associated with the class in which they reside i.e. they can be called even without creating an instance of the class.
  • They are designed with the aim to be shared among all objects created from the same class.
  • Static methods can not be overridden. But can be overloaded since they are resolved using static binding by the compiler at compile time.

The following table shows the list of Static Methods provided by Optional Class : 

Image

Instance Methods: Instance methods are methods that require an object of its class to be created before it can be called. To invoke an instance method, we have to create an Object of the class within which it is defined. 

Syntax : 

public void geek(String name)
{
// code to be executed....
}
// Return type can be int, float String or user defined data type.

Important Points: Instance Methods can be called within the same class in which they reside or from the different classes defined either in the same package or other packages depending on the access type provided to the desired instance method. Below given are some important points regarding Instance Methods : 

  • Instance method(s) belong to the Object of the class, not to the class i.e. they can be called after creating the Object of the class.
  • Every individual object created from the class has its own copy of the instance method(s) of that class.
  • They can be overridden since they are resolved using dynamic binding at run time.

The following table shows the list of Instance Methods provided by the Optional Class : 

ImageImage

Concrete Methods: A concrete method means, the method has a complete definition but can be overridden in the inherited class. If we make this method final, then it can not be overridden. Declaring method or class “final” means its implementation is complete. It is compulsory to override abstract methods. Concrete Methods can be overridden in the inherited classes if they are not final. The following table shows the list of Concrete Methods provided by the Optional Class :

ImageImage

Below given are some examples :

Example 1 : 

Java
// Java program to illustrate
// optional class methods

import java.util.Optional;

class GFG {

    // Driver code
    public static void main(String[] args)
    {

        // creating a string array
        String[] str = new String[5];

        // Setting value for 2nd index
        str[2] = "Geeks Classes are coming soon";

        // It returns an empty instance of Optional class
        Optional<String> empty = Optional.empty();
        System.out.println(empty);

        // It returns a non-empty Optional
        Optional<String> value = Optional.of(str[2]);
        System.out.println(value);
    }
}

Output
Optional.empty
Optional[Geeks Classes are coming soon]

Example 2 : 

Java
// Java program to illustrate
// optional class methods

import java.util.Optional;

class GFG {

    // Driver code
    public static void main(String[] args)
    {

        // creating a string array
        String[] str = new String[5];

        // Setting value for 2nd index
        str[2] = "Geeks Classes are coming soon";

        // It returns a non-empty Optional
        Optional<String> value = Optional.of(str[2]);

        // It returns value of an Optional.
        // If value is not present, it throws
        // an NoSuchElementException
        System.out.println(value.get());

        // It returns hashCode of the value
        System.out.println(value.hashCode());

        // It returns true if value is present,
        // otherwise false
        System.out.println(value.isPresent());
    }
}

Output
Geeks Classes are coming soon
1967487235
true



Previous Article
Next Article

Similar Reads

How to avoid NullPointerException in Java using Optional class?
In order to learn how to avoid an error, we must first understand the error. NullPointerException NullPointerException is a RuntimeException. In Java, a special null value can be assigned to an object reference. NullPointerException is thrown when program attempts to use an object reference that has the null value. These can be: Invoking a method f
4 min read
Optional Class | isPresent() function
The isPresent() function in Optional Class is used to evaluate whether the value if assigned to variable is present or not. Syntax value.isPresent() Returns: It returns true if value is assigned otherwise false Examples: Input : Optional value1 = Optional.ofNullable(10); value1.isPresent() Output : True Input : Optional value2 = Optional.ofNullable
1 min read
Optional stream() method in Java with examples
The stream() method of java.util.Optional class in Java is used to get the sequential stream of the only value present in this Optional instance. If there is no value present in this Optional instance, then this method returns returns an empty Stream. Syntax: public Stream&lt;T&gt; stream() Parameters: This method do not accept any parameter. Retur
2 min read
Optional isPresent() method in Java with examples
The isPresent() method of java.util.Optional class in Java is used to find out if there is a value present in this Optional instance. If there is no value present in this Optional instance, then this method returns false, else true. Syntax: public boolean isPresent() Parameters: This method do not accept any parameter. Return value: This method ret
2 min read
Optional empty() method in Java with examples
The empty() method of java.util.Optional class in Java is used to get an empty instance of this Optional class. This instance do not contain any value. Syntax: public static &lt;T&gt; Optional&lt;T&gt; empty() Parameters: This method accepts nothing. Return value: This method returns an empty instance of this Optional class. Below programs illustra
1 min read
Optional filter() method in Java with examples
The filter() method of java.util.Optional class in Java is used to filter the value of this Optional instance by matching it with the given Predicate, and then return the filtered Optional instance. If there is no value present in this Optional instance, then this method returns an empty Optional instance. Syntax: public Optional&lt;T&gt; filter(Pr
2 min read
Optional of() method in Java with examples
The of() method of java.util.Optional class in Java is used to get an instance of this Optional class with the specified value of the specified type. Syntax: public static &lt;T&gt; Optional&lt;T&gt; of(T value) Parameters: This method accepts value as parameter of type T to create an Optional instance with this value. Return value: This method ret
1 min read
Optional ofNullable() method in Java with examples
The ofNullable() method of java.util.Optional class in Java is used to get an instance of this Optional class with the specified value of the specified type. If the specified value is null, then this method returns an empty instance of the Optional class. Syntax: public static &lt;T&gt; Optional&lt;T&gt; ofNullable(T value) Parameters: This method
2 min read
Optional get() method in Java with examples
The get() method of java.util.Optional class in Java is used to get the value of this Optional instance. If there is no value present in this Optional instance, then this method throws NullPointerException. Syntax: public T get() Parameters: This method do not accept any parameter. Return value: This method returns the value of this instance of the
2 min read
Optional toString() method in Java with examples
The toString() method of java.util.Optional class in Java is used to get the string representation of this Optional instance. Syntax: public String toString() Parameters: This method accepts nothing. Return value: This method returns the string representation of this Optional instance. Below programs illustrate toString() method: Program 1: // Java
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg