Open In App

Java Long equals() method with Examples

Last Updated : 05 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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


Similar Reads

Java Guava | Longs.checkedSubtract(long a, long b) method with Examples
The checkedSubtract(long a, long b) is a method of Guava's LongMath Class which accepts two parameters a and b, and returns their difference. Syntax: public static long checkedSubtract(long a, long b) Parameters: The method accepts two long values a and b and computes their difference. Return Value: The method returns the difference of long values
2 min read
Duration ofSeconds(long, long) method in Java with Examples
The ofSeconds(long, long) method of Duration Class in java.time package is used to get a duration in a 1 second format. In this method, the seconds are calculated as total seconds in 1 second format, i.e. 1 second per second. The second parameter nanoAdjustment is used to adjust the seconds in nanoSeconds unit. Syntax: public static Duration ofSeco
2 min read
Java Guava | LongMath.checkedAdd(long a, long b) method with Examples
The checkedAdd(long a, long b) is a method of Guava's LongMath Class which accepts two parameters a and b, and returns their sum. Syntax: public static long checkedAdd(long a, long b) Parameters: The method accepts two long values a and b and computes their sum. Return Value: The method returns the sum of long values passed to it, provided it does
2 min read
Java Guava | Longs.indexOf(long[] array, long[] target) method with Examples
Longs.indexOf(long[] array, long[] target) method of Guava's Longs Class accepts two parameters array and target. If the target exists within the array, the method returns the start position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(long[] array, long[] target) P
3 min read
Java Guava | LongMath.divide(long, long, RoundingMode) method with Examples
The divide(long p, long q, RoundingMode mode) method of Guava's LongMath Class accepts three parameters and calculates the result of dividing first parameter by second parameter, rounded according to the rounding mode specified by the third parameter. Syntax: public static long divide(long p, long q, RoundingMode mode) Parameters: The method takes
3 min read
Java Guava | Longs.indexOf(long[] array, long target) method with Examples
Longs.indexOf(long[] array, long target) method of Guava's Longs Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(long[] array, long target) Parameters:
3 min read
Java Guava | gcd(long a, long b) of LongMath Class with Examples
The method gcd(long a, long b) of Guava's LongMath Class returns the greatest common divisor of two parameters a and b. Syntax: public static long gcd(long a, long b) Parameters: This method accepts two parameters a and b of the long type of whose GCD is to be found. Return Type: This method returns the largest positive long value that divides both
2 min read
Java Guava | mean(long x, long y) of LongMath Class with Examples
The mean(long x, long y) method of Guava's LongMath Class accepts two parameters x and y and calculates arithmetic mean of them rounded towards negative infinity. This method is overflow resilient. Syntax: public static long mean(long x, long y) Parameters: This method accepts two parameters x and y which are of long type. Return Value: The method
2 min read
Java Guava | mod(long x, long m) of LongMath Class with Examples
The mod(long x, long m) method of Guava's LongMath Class accepts two parameters x and m, and used to calculate the value of x modulus under m. Syntax: public static long mod(long x, long m) Parameters: This method accepts two parameters x and m which are of long type to calculate x modulo m. Return Value: The method returns x mod m that will be a n
2 min read
Java Math addExact(long x, long y) method
The java.lang.Math.addExact() is a built-in math function in java which returns the sum of its arguments. It throws an exception if the result overflows a long.As addExact(long x, long y) is static, so object creation is not required. Syntax : public static long addExact(long x, long y) Parameter : a : the first value b : the second value Return :
1 min read
Practice Tags :