Easy Tutorial
❮ Net Filetime Java String Length ❯

Java hashCode() Method

Java String Class


The hashCode() method is used to return the hash code of a string.

The hash code for a string object is computed using the following formula:

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

Using int arithmetic, where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation. The hash value of an empty string is 0.

Syntax

public int hashCode()

Parameters

Return Value

Returns the hash code value of the object.

Example

public class Test {
        public static void main(String args[]) {
                String Str = new String("www.tutorialpro.org");
                System.out.println("Hash code of the string is :" + Str.hashCode() );
        }
}

The output of the above program is:

Hash code of the string is :321005537

Java String Class

❮ Net Filetime Java String Length ❯