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