Short hashCode() in Java

hashCode(): This method is available in java.lang.Short class of Java.

Approach 1: When the method does not take any argument.

Syntax:

int java.lang.Short.hashCode()

This method returns a hash code for this Short; equal to the result of invoking intValue().

Parameters: NA

Returns: a hash code value for this Short.

Exceptions: NA

Java

public class ShorthashCode {
    public static void main(String[] args) {

        Short short1 = 1;

        System.out.println(short1.hashCode());
    }
}

Output:

1


Approach 2: When the method takes one argument.

Syntax:

int java.lang.Short.hashCode(short value)

This method takes one argument of type short as its parameter. This method returns a hash code for a short value; compatible with Short.hashCode().

Parameters: One parameter is required for this method.

value: the value to the hash.

Returns: a hash code value for a short value.

Exceptions: NA

Java

public class ShorthashCode2 {
    public static void main(String[] args) {

        short value = 4;
        System.out.println(Short.hashCode(value));
    }
}

Output:

4

No comments:

Post a Comment