Thursday, February 19, 2026
HomeLanguagesJavaOptionalLong equals() method in Java with examples

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 ==.

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
// OptionalLong.equals(Object obj) method
  
import java.util.OptionalLong;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Create first OptionalLong object
        OptionalLong opLong1
            = OptionalLong.of(45213246);
  
        System.out.println("OptionalLong 1: "
                           + opLong1.toString());
  
        // Create second OptionalLong object
        OptionalLong opLong2
            = OptionalLong.of(45213246);
  
        System.out.println("OptionalLong 2: "
                           + opLong2.toString());
  
        // Check if these two objects are equal
        // using equals(Object obj)
        System.out.println("Are both objects equal: "
                           + opLong1.equals(opLong2));
    }
}


Output:

OptionalLong 1: OptionalLong[45213246]
OptionalLong 2: OptionalLong[45213246]
Are both objects equal: true

Program 2:




// Java program to demonstrate
// OptionalLong.equals(Object obj) method
  
import java.util.OptionalLong;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // Create first OptionalLong object
        OptionalLong opLong1
            = OptionalLong.of(317946492);
  
        System.out.println("OptionalLong 1: "
                           + opLong1.toString());
  
        // Create second OptionalLong object
        OptionalLong opLong2
            = OptionalLong.of(45213246);
  
        System.out.println("OptionalLong 2: "
                           + opLong2.toString());
  
        // Check if these two objects are equal
        // using equals(Object obj)
        System.out.println("Are both objects equal: "
                           + opLong1.equals(opLong2));
    }
}


Output:

OptionalLong 1: OptionalLong[317946492]
OptionalLong 2: OptionalLong[45213246]
Are both objects equal: false

References: https://docs.oracle.com/javase/10/docs/api/java/util/OptionalLong.html#equals?(Object obj)

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS