Tutorial Gateway

  • C
  • C#
  • Python
  • SQL
  • Java
  • JS
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs
  • MySQL

Find String hashCode in Java

The Java hashCode method is one of the Java String Methods, which is to find and return the hashCode of the User specified string. The hashCode value of an empty string is Zero. In this article, we will show 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. The syntax of the string hashCode in Java Programming language is

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

In this program, we use the string hashCode method to find the hashCode value of the user-specified string.

//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);
	}
}
Find String hashCode in Java 1

The following statements will find the string hashCode

int a = str.hashCode();

If you observe the above Java screenshot, the above statement returns 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 every character.

The following statements will find the Java string hashCode of the strings and then assign the hashcode 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();

The following System.out.println statements in this program will print the String Method 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);

Filed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial

Copyright © 2021· All Rights Reserved by Suresh.
About | Contact | Privacy Policy