VOOZH about

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

⇱ Optional equals() method in Java with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Optional equals() method in Java with Examples

Last Updated : 12 Jul, 2025
The equals() method of java.util.Optional class in Java is used to check for equality of this Optional with the specified Optional. This method takes an Optional instance and compares it with this Optional and returns a boolean value representing the same. Syntax:
public boolean equals(Object obj)
Parameter: This method accepts a parameter obj which is the Optional to be checked for equality with this Optional. Return Value: This method returns a boolean which tells if this Optional is equal to the specified Object. Exception: This method do not throw any Exception. Program 1:
Output:
Optional 1: Optional[456]
Optional 2: Optional[456]
Comparing Optional 1 and Optional 2: true
Program 2:
Output:
Optional 1: Optional[456]
Optional 2: Optional.empty
Comparing Optional 1 and Optional 2: false
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Optional.html#equals-java.lang.Object-
Comment