Java Long equals() method with Examples

The java.lang.Long.equals() is a built-in function in java that compares this object to the specified object. The result is true if and only if the argument is not null and is a Long object that contains the same long value as this object. It returns false if both the objects are not same. In all other cases, compareTo method should be preferred.

Syntax:

public boolean equals(Object obj)  

Parameter: 
obj - The passed object is the object that is to be compared with. 

Returns:
The function returns a boolean value after comparing with the object passed in the parameter. It returns true if and only if the argument is not null and is a Long object that contains the same long value as this object. It returns false if the object are not same.



Program 1: The program below demonstrates the working of function.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// of java.lang.Long.equals() method
import java.lang.Math;
  
class Gfg1 {
  
    public static void main(String args[])
    {
  
        // when two objects are different
        Long obj1 = new Long(123123);
        Long obj2 = new Long(164165);
        System.out.print("Object1 & Object2: ");
        if (obj1.equals(obj2))
            System.out.println("Equal");
        else
            System.out.println("Not equal");
  
        // when two objects are equal
        obj1 = new Long(12345);
        obj2 = new Long(12345);
        System.out.print("Object1 & Object2: ");
        if (obj1.equals(obj2))
            System.out.print("Equal");
        else
            System.out.print("Not Equal");
    }
}

chevron_right


Output:

object1 and object2 are not equal
object1 and object2 are equal

Program 2: The program below demonstrates the working of function when no argument is passed

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// of java.lang.Long.equals() method
import java.lang.Math;
  
class Gfg1 {
  
    // driver code
    public static void main(String args[])
    {
  
        // when no argument is passed
        Long obj1 = new Long(124);
        Long obj2 = new Long(167);
        System.out.print("Object1 & Object2: ");
        if (obj1.equals())
            System.out.println("Equal");
        else
            System.out.println("Not Equal");
    }
}

chevron_right


Output:

prog.java:15: error: no suitable method found for equals(no arguments)
      if(obj1.equals())
             ^
    method Object.equals(Object) is not applicable
      (actual and formal argument lists differ in length)
    method Long.equals(Object) is not applicable
      (actual and formal argument lists differ in length)
1 error

Program 3: The program below demonstrates the working of function when anything other than the object is passed in an argument

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// of java.lang.Long.equals() method
import java.lang.Math;
  
class Gfg1 {
  
    // driver code
    public static void main(String args[])
    {
  
        // when anything other than argument is passed
  
        Long obj1 = new Long(124);
  
        System.out.print("Object1 & Object2: ");
        if (obj1.equals("gfg"))
            System.out.println("Equal");
        else
            System.out.println("Not Equal");
    }
}

chevron_right


Output:

Object1 & Object2:  Not Equal


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.