VOOZH about

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

⇱ OptionalInt equals() method in Java with examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

OptionalInt equals() method in Java with examples

Last Updated : 1 May, 2019
OptionalInt help us to create an object which may or may not contain a Int value. The equals(Object obj) method help us to compare this OptionalInt object with the passed object as a parameter and it returns true if objects are equal. The other object is considered equal to this OptionalInt if:
  • it is also an OptionalInt and;
  • both instances have no value present or;
  • the present values are "equal to" each other via ==.
Syntax:
public boolean equals(Object obj)
Parameters: This method accepts an obj which is an object to be tested for equality. Return value: This method returns true if the other object is "equal to" this object otherwise false. Below programs illustrate equals(Object obj) method: Program 1:
Output:
OptionalInt 1: OptionalInt[7258]
OptionalInt 2: OptionalInt[7258]
Are both objects equal: true
Program 2:
Output:
OptionalInt 1: OptionalInt[5422]
OptionalInt 2: OptionalInt[2737]
Are both objects equal: false
References: https://docs.oracle.com/javase/10/docs/api/java/util/OptionalInt.html#equals?(Object obj)
Comment