Home Tutorial Java StringExamples java.lang.String.hashCode()

 
 

java.lang.String.hashCode()
Posted on: March 22, 2005 at 12:00 AM
int hashCode() method in Java returns a hash code for the given string but it can return zero as well if the given string is empty.

int hashCode() method in Java returns a hash code for the given string but it can return zero as well if the given string is empty. It can be computed as ..

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

Where..

i -> is a first character

n-> length of the string

^ indicates exponentiation

See the example given below that explains how to get the hashCode for any given string in Java.

a simple hashCode example:

public class StringTrim {

public static void main(String[] args) {

String str = "Java Examples Code";

System.out.println("Corresponding hash code value returned is: "

+ str.hashCode());

}

}

the output is:

Corresponding hash code value returned is: 1275699718

Related Examples:
Java BigDecimal hashCode example

 

Related Tags for java.lang.String.hashCode():


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.