VOOZH about

URL: https://www.geeksforgeeks.org/java/optional-tostring-method-in-java-with-examples/

⇱ Optional toString() method in Java with examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Optional toString() method in Java with examples

Last Updated : 12 Jul, 2025
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:
Output:
String Representation: Optional[452146]
Program 2:
Output:
String Representation: Optional.empty
References: https://docs.oracle.com/javase/9/docs/api/java/util/Optional.html#toString--
Comment