Character hashCode() in Java

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

Approach 1: When the method takes one argument.

Syntax:

int java.lang.Character.hashCode()

This method returns a hash code for this Character.

Parameters: NA

Returns: a hash code value for this Character.

Exceptions: NA

Java

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

        Character c = 'a';

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

Output:

97


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

Syntax:

int java.lang.Character.hashCode(char value)

This method takes one argument of type char as its parameter. This method returns a hash code for a char value.

Parameters: value The char for which to return a hash code.

Returns: a hash code value for a char value.

Exceptions: NA (This method does not throw any exceptions).

Java

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

        Character c = 'a';

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

Output:

109

No comments:

Post a Comment