Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Java Long equals() method with Examples

  • Last Updated : 05 Dec, 2018

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:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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.






// 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");
    }
}

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




// 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");
    }
}

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




// 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");
    }
}

Output:

Object1 & Object2:  Not Equal



My Personal Notes arrow_drop_up
Recommended Articles
Page :