Home >>Java String Methods >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.
public int hashCode()
None
Returns - It returns an int value which represents the hash code of the string
public class hashCodeClass1
{
public static void main(String[] args)
{
String Name = "Phptpoint";
System.out.println(Name.hashCode());
}
}
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() );
}
}