Home >>Java String Methods >Java String hashCode() Method

Java String hashCode() Method

Java String hashCode() Method

The hashCode() method in Java String is used to returns the hash code of a string.

You can compute has code string object like this:

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

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

Syntax

public int hashCode()

Parameter

None

Technical Details

Returns - It returns an int value which represents the hash code of the string

Java String hashCode() Method Example 1


public class hashCodeClass1 
{
  public static void main(String[] args) 
  {
    String Name = "Phptpoint";
    System.out.println(Name.hashCode());
  }
}

Output: 2045203700

Java String hashCode() Method Example 2


public class hashCodeClass2 
{
   public static void main(String args[]) 
   {
      String Strln1 = new String("Welcome to phptpoint.com");
      System.out.println("Hashcode for Str :" + Strln1.hashCode() );
   }
}


Output: Hashcode for Str :1092877792