java.lang.String hashCode()

Description :

This java tutorial shows how to use the hashCode() method of java.lang.String class. This method returns an int datatype which corresponds to the hash code of the string. The following formula is being used in the computation:

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

Method Syntax :

public int hashCode()

Parameter Input :

  • None

Method Returns :

This method returns an int datatype which corresponds to the hash code of the String.

Java Code Example :

This example source code demonstrates the use of hashCode() method of String class. Basically this source code just prints the Hash Code equivalent of our String.

package com.javatutorialhq.java.tutorial.string;

/*
 * Example source code on printing the hashCode of String
 */
public class StringHashCode {

    public static void main(String[] args) {
        String stringExample = "This is sampel String";
        System.out.println("Hash Code:" + stringExample.hashCode());
    }

}

Sample Output :

Running the hashCode() method example source code of java.lang.String class will give you the following output

string hashcode method example

string hashcode method example

Suggested Reading List :

References :