Character.hashCode() in Java with examples

Java.lang.Character.hashCode() is a built-in method in Java which returns a hash code for this Character. The returned hash code is equal to the result of invoking charValue().

Syntax:

public int hashCode()

This function does not accepts any parameter.

Return Value: This method returns a hash code value for this Character.

Below programs illustrate the Java.lang.Character.hashCode() function:

Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// function when the value passed in the parameter
// is a character 
import java.lang.*;
  
public class Gfg {
  
    public static void main(String[] args)
    {
        // parameter ch
        char ch = 'B';
        // assigns character values
        Character c = Character.valueOf(ch);
         
      
        // assign hashcodes of c1, c2 to i1, i2
        int i = c.hashCode();
          
        // prints the character values
        System.out.println("Hashcode of " + ch + " is " + i);
    }
}

chevron_right


Output:

Hashcode of B is 66

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// function when the value passed in the parameter
// is a number 
import java.lang.*;
  
public class Gfg {
  
    public static void main(String[] args)
    {
        // parameter ch
        char ch = '6';
        // assigns character values
        Character c = Character.valueOf(ch);
         
      
        // assign hashcodes of ch 
        int i = c.hashCode();
          
        // prints the character values
        System.out.println("Hashcode of " + ch + " is " + i);
    }
}

chevron_right


Output:

Hashcode of 6 is 54

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.