java tutorial - Java Strings hashCode() method - java programming - learn java - java basics - java for beginners



Java strings hashcode

Learn Java - Java tutorial - Java strings hashcode - Java examples - Java programs

Description

Java Strings hashCode() method - it returns a hash code for the given string. How the hash code is calculated for the object of String:

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
click below button to copy the code. By - java tutorial - team
  • The use of integer arithmetic, where s [i] is the i-th character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of empty string is zero).

Syntax

Syntax of this method:

public int hashCode()
click below button to copy the code. By - java tutorial - team

Options

Detailed information about the parameters:

  • This is the default method, and it will not accept any parameters.

Return value

  • In Java, hashCode () returns the hash value for this object.

Sample Code

import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str = new String("Welcome to wikitechy.com");
      System.out.println("Hashcode for Str :" + Str.hashCode() );
   }
}
click below button to copy the code. By - java tutorial - team

Output

Hashcode for Str :1639280703

Related Searches to Java Strings hashCode() method