The Java hashCode method is one of the Java String Method which is used to find and return the hashCode of the User specified string.
In this article we will show you, How to find String hashCode in Java Programming language with example.
The formula behind the hashcode is: s[0]*31(n-1) + s[1]*31(n-2) + .. s(n-2). Here, s[i] is the ith character of the user specified string and n is the string length.
NOTE: The hashCode value of an empty string is Zero.
Syntax to find String hashCode in Java
The basic syntax of the string hashCode in Java Programming language is as shown below.
public int hashCode() // It will return the integer Value as Output //In order to use in program String_Object.hashCode()
- String_Object: Please specify the valid String Object.
Example to find String hashCode in Java
The Java string hashCode method is used to return the hashCode value of the user specified string. In this Java program, We are going to find the same.
JAVA CODE
//How to find String hashCode in Java package StringFunctions; public class HashcodeMethod { public static void main(String[] args) { String str = "Hi"; int a = str.hashCode(); int b = "Hello".hashCode(); int c = "Java Programming".hashCode(); int d = "hello world".hashCode(); int e = "tutorial gateway".hashCode(); System.out.println("Hashcode of the String str = " + a); System.out.println("Hashcode of the String = " + b); System.out.println("Hashcode of the String = " + c); System.out.println("Hashcode of the String = " + d); System.out.println("Hashcode of the String = " + e); } }
OUTPUT
ANALYSIS
First we declared the String variable and assigned a value using following statement
String str = "Hi";
The following statements will find the Java string hashCode method of the above specified string.
int a = str.hashCode();
If you observe the above screenshot, above statement is returning 2337 as output. Let us find the same using the standard formula:
HashCode = s[0]*31(n-1) + s[1]*31(n-2) + .. s(n-2)
As we all know that the Character at position 0 is H, Character at position 1 is i and the string length is 2
==> H*31(2-1) + i*31(2-2)
As we all know that, ASCII code of H is 72 and i is 105. It means,
==> 72 * 31 + 105 * 1 (Anything Power 0 is 1)
==> 2232 + 105 = 2337
TIP: Please refer ASCII Table to check the character codes of each and every character.
Following statements will find the Java string hashCode of the strings and then assign the hascode values of those strings to the integer variables b, c, d and e.
int b = "Hello".hashCode(); int c = "Java Programming".hashCode(); int d = "hello world".hashCode(); int e = "tutorial gateway".hashCode();
Following System.out.println statements will print the output
System.out.println("Hashcode of the String str = " + a); System.out.println("Hashcode of the String = " + b); System.out.println("Hashcode of the String = " + c); System.out.println("Hashcode of the String = " + d); System.out.println("Hashcode of the String = " + e);
Thank You for Visiting Our Blog