Signup/Sign In

Java Character hashCode(char value) Method

The hashCode(char ch) method is compatible with Java 1.8 or more version and is a part of the Character class of the java.lang package. It is used to return the hash code of the char value passed as the argument.

This method is compatible with Character.hashCode() method of Character class.


public static int hashCode(char ch)  


The parameter includes the char value of which hash code is to be generated.


The unique integer value (hash code) associated with the char value passed as an argument.

Example 1:

Here, using the hashCode(char ch) method hashcode is returned of the char value passed.

import java.lang.Character;
public class StudyTonight 
	public static void main(String[] args) 
		int hv1 = Character.hashCode('J'); //generate the hashcode of the passed argument
		int hv2 = Character.hashCode('w');
		System.out.println("Hash code Value is: " + hv1);
		System.out.println("Hash code Value is: " + hv2);

Hash code Value is: 74
Hash code Value is: 119

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent hash code.

import java.util.Scanner;
public class StudyTonight 
	public static void main(String[] args) 
			System.out.print("Enter the value: ");
			Scanner sc = new Scanner(System. in );
			Character ch =;
			int hv = Character.hashCode(ch); // Returning hash code value for this object
			System.out.println("Hash code is: " + hv);
		catch(Exception e) 
			System.out.println("Invalid Input!!");

Enter the value: y
Hash code is: 121
Enter the value: 6
Hash code is: 54

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.

About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.