OptionalLong equals() method in Java with examples
OptionalLong help us to create an object which may or may not contain a Long value. The equals(Object obj) method help us to compare this OptionalLong object with the passed object as a parameter and it returns true if objects are equal.
The other object is considered equal to this OptionalLong if:
- it is also an OptionalLong and;
- both instances have no value present or;
- the present values are “equal to” each other via ==.
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:
OptionalLong 1: OptionalLong OptionalLong 2: OptionalLong Are both objects equal: true
OptionalLong 1: OptionalLong OptionalLong 2: OptionalLong Are both objects equal: false