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.
// 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
Recommended Posts:
- Java Guava | LongMath.divide(long, long, RoundingMode) method with Examples
- Java Guava | Longs.checkedSubtract(long a, long b) method with Examples
- Java Guava | LongMath.checkedAdd(long a, long b) method with Examples
- Java Guava | Longs.indexOf(long[] array, long target) method with Examples
- Java Guava | Longs.indexOf(long[] array, long[] target) method with Examples
- Duration ofSeconds(long, long) method in Java with Examples
- OptionalLong of(long) method in Java with examples
- Java Long decode() method with Examples
- Java Guava | mod(long x, long m) of LongMath Class with Examples
- Java Guava | mean(long x, long y) of LongMath Class with Examples
- Java Guava | gcd(long a, long b) of LongMath Class with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.lowestOneBit() 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.



