Optional toString() method in Java with examples - GeeksforGeeks

Optional toString() method in Java with examples

Last Updated : 30 Oct, 2026

The toString() method of the Optional class in Java returns a string representation of an Optional object. It helps developers identify whether the Optional contains a value or is empty, making it useful for debugging and logging.

  • It is defined in the java.util.Optional class and returns a String.
  • If a value is present, the result typically follows the format Optional[value].
  • If no value is present, it returns Optional.empty.

Example: Program to demonstrates how the toString() method represents an Optional containing a value and an empty Optional.

Java
import java.util.Optional;

class Geeks{
    public static void main(String[] args) {

        // Create an Optional containing a value
        Optional<String> opt1 = Optional.of("GeeksforGeeks");

        // Create an empty Optional
        Optional<String> opt2 = Optional.empty();

        // Display string representations
        System.out.println(opt1.toString());
        System.out.println(opt2.toString());
    }
}

Output
Optional[GeeksforGeeks]
Optional.empty

Explanation:

  • Optional.of("GeeksforGeeks") creates an Optional containing the specified string, so toString() returns Optional[GeeksforGeeks].
  • Optional.empty() creates an Optional without a value, so toString() returns Optional.empty.

Syntax

public String toString()

  • Parameters: This method does not accept any parameters.
  • Return Value: It returns a String representing the Optional object and its contained value, if present.

How Optional toString() Method Works

The toString() method checks whether the Optional contains a value and produces a readable string representation.

Optional
  • Present Value: The method includes the contained object's string representation inside Optional[...].
  • Empty Optional: The method returns Optional.empty to indicate that no value is stored.

Examples of Optional toString() Method

Example 1: Using toString() with an Integer Value

The following program demonstrates the toString() method with an Optional containing an integer.

Java
import java.util.Optional;

class Geeks{
    public static void main(String[] args) {

        // Create an Optional containing an integer
        Optional<Integer> number = Optional.of(100);

        // Convert the Optional to a string
        String result = number.toString();

        // Display the result
        System.out.println(result);
    }
}

Output
Optional[100]

Explanation: The Optional contains the integer 100, so the method returns its string representation as Optional[100].

Example 2: Using toString() with Optional.ofNullable()

The Optional.ofNullable() method creates an empty Optional when the supplied value is null. The following program demonstrates its string representation.

Java
import java.util.Optional;

class Geeks{
    
    public static void main(String[] args){

        // Create an Optional from a null value
        String name = null;
        Optional<String> optionalName = Optional.ofNullable(name);

        // Display the Optional representation
        System.out.println(optionalName.toString());
    }
}

Output
Optional.empty

Explanation: Since the supplied value is null, Optional.ofNullable() returns an empty Optional, and toString() displays Optional.empty.

Variants of Optional toString() Method

Java provides specialized Optional classes for primitive values: OptionalInt, OptionalLong, and OptionalDouble. Each class has its own toString() method that returns a readable representation of the contained value or indicates that it is empty.

1. OptionalInt toString() Method

The OptionalInt.toString() method returns a string representation of an OptionalInt object that may contain a primitive int value.

Example: Program to demonstrate the toString() method with a present and an empty OptionalInt.

Java
import java.util.OptionalInt;

class Geeks{
    public static void main(String[] args){

        // Create an OptionalInt with a value
        OptionalInt num1 = OptionalInt.of(100);

        // Create an empty OptionalInt
        OptionalInt num2 = OptionalInt.empty();

        // Display string representations
        System.out.println(num1.toString());
        System.out.println(num2.toString());
    }
}

Output
OptionalInt[100]
OptionalInt.empty

Explanation: The first OptionalInt contains the value 100, while the second is empty. The method returns their respective string representations.

2. OptionalLong toString() Method

The OptionalLong.toString() method returns a string representation of an OptionalLong object that may contain a primitive long value.

Example: Program to demonstrate the toString() method with a present and an empty OptionalLong.

Java
import java.util.OptionalLong;

class Geeks{
    public static void main(String[] args){

        // Create an OptionalLong with a value
        OptionalLong num1 = OptionalLong.of(100000L);

        // Create an empty OptionalLong
        OptionalLong num2 = OptionalLong.empty();

        // Display string representations
        System.out.println(num1.toString());
        System.out.println(num2.toString());
    }
}

Output
OptionalLong[100000]
OptionalLong.empty

Explanation: The method displays OptionalLong[100000] for the stored long value and OptionalLong.empty for the empty object.

3. OptionalDouble toString() Method

The OptionalDouble.toString() method returns a string representation of an OptionalDouble object that may contain a primitive double value.

Example: Program to demonstrate the toString() method with a present and an empty OptionalDouble.

Java
import java.util.OptionalDouble;

class Geeks{
    public static void main(String[] args) {

        // Create an OptionalDouble with a value
        OptionalDouble num1 = OptionalDouble.of(25.5);

        // Create an empty OptionalDouble
        OptionalDouble num2 = OptionalDouble.empty();

        // Display string representations
        System.out.println(num1.toString());
        System.out.println(num2.toString());
    }
}

Output
OptionalDouble[25.5]
OptionalDouble.empty

Explanation: The method returns OptionalDouble[25.5] when a value is present and OptionalDouble.empty when no value is stored.

Important Points About Optional toString()

  • Available Since Java 8: The method belongs to java.util.Optional and overrides the toString() method of the Object class.
  • Does Not Modify the Optional: It only returns a string representation without changing the contained value.
  • Not Intended for Value Extraction: Use orElse(), orElseGet(), or orElseThrow() when the actual value is required.
  • Output Format Is Not Guaranteed: Although Optional[value] and Optional.empty are commonly observed, Java does not guarantee these exact formats across implementations or versions.
Comment