OptionalInt equals() method in Java with examples
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:
// Java program to demonstrate // OptionalInt.equals(Object obj) method import java.util.OptionalInt; public class GFG { public static void main(String[] args) { // Create first OptionalInt object OptionalInt opInt1 = OptionalInt.of(7258); System.out.println("OptionalInt 1: " + opInt1.toString()); // Create second OptionalInt object OptionalInt opInt2 = OptionalInt.of(7258); System.out.println("OptionalInt 2: " + opInt2.toString()); // Check if these two objects are equal // using equals(Object obj) System.out.println("Are both objects equal: " + opInt1.equals(opInt2)); } } |
OptionalInt 1: OptionalInt[7258] OptionalInt 2: OptionalInt[7258] Are both objects equal: true
Program 2:
// Java program to demonstrate // OptionalInt.equals(Object obj) method import java.util.OptionalInt; public class GFG { public static void main(String[] args) { // Create first OptionalInt object OptionalInt opInt1 = OptionalInt.of(5422); System.out.println("OptionalInt 1: " + opInt1.toString()); // Create second OptionalInt object OptionalInt opInt2 = OptionalInt.of(2737); System.out.println("OptionalInt 2: " + opInt2.toString()); // Check if these two objects are equal // using equals(Object obj) System.out.println("Are both objects equal: " + opInt1.equals(opInt2)); } } |
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)
Recommended Posts:
- OptionalInt of(int) method in Java with examples
- OptionalInt stream() method in Java with examples
- OptionalInt hashCode() method in Java with examples
- OptionalInt isPresent() method in Java with examples
- OptionalInt orElseGet() method in Java with examples
- OptionalInt orElse(int) method in Java with examples
- OptionalInt ifPresentOrElse() method in Java with examples
- OptionalInt toString() method in Java with examples
- OptionalInt orElseThrow() method in Java with examples
- OptionalInt empty() method in Java with examples
- OptionalInt getAsInt() method in Java with examples
- OptionalInt ifPresent(IntConsumer) method in Java with examples
- OptionalInt orElseThrow(Supplier) method in Java with examples
- Set equals() method in Java with Examples
- Map equals() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


