Java.lang Package Classes

Java Long - hashCode() Method



The java.lang.Long.hashCode() method returns a hash code for this Long.

Syntax

public int hashCode()

Parameters

No parameter is required.

Return Value

Returns a hash code value for this Long object.

Exception

NA.

Example:

In the example below, the java.lang.Long.hashCode() method returns a hash code for the given Long object.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating Long objects
    Long val1 = 1025l;
    Long val2 = -1025l;

    //printing hashcode of the Long objects
    System.out.println("hashCode of the val1 is: " + val1.hashCode()); 
    System.out.println("hashCode of the val2 is: " + val2.hashCode());   
  }
}

The output of the above code will be:

hashCode of the val1 is: 1025
hashCode of the val2 is: 1024

❮ Java.lang - Long