Please note, this is a STATIC archive of website www.w3schools.com from 05 May 2020, cach3.com does not collect or store any user information, there is no "phishing" involved.
THE WORLD'S LARGEST WEB DEVELOPER SITE

Java String hashCode() Method

❮ String Methods


Example

Return the hash code of a string:

public class MyClass {
  public static void main(String[] args) {
    String myStr = "Hello";
    System.out.println(myStr.hashCode());
  }
}

Run example »


Definition and Usage

The hashCode() method returns the hash code of a string.

The hash code for a String object is computed like this:

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation.


Syntax

public int hashCode()

Parameter Values

None.

Technical Details

Returns: An int value, representing the hash code of the string

❮ String Methods