×

Java String hashCode() method

Java String hashCode() method returns hash code for current String.

hash code for string object is computed as

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 the empty string is zero.)

Syntax:

public int hashCode()

Return: an int datatype i.e. hashcode of the String.

Java String hashCode() example 1:

public class JavaStringHashCodeEx1
{
    public static void main(String[] args) {
        String s1 = "tutorialandexample.com";
        System.out.println("Hash Code:" + s1.hashCode());
    }
}

Output:

Hash Code:1997172644

Java String hashCode() example 2: Two String having same hash code

public class JavaStringHashCodeEx2
{
    public static void main(String[] args)
{
        String s1 = "tutorialandexample.com";
        String s2 = "tutorialandexample.com";
        System.out.println("Hash Code:" + s1.hashCode());
         System.out.println("Hash Code:" + s2.hashCode());
    }
}

Output:

Hash Code:1997172644
Hash Code:1997172644

Java String hashCode() example 3:

public class JavaStringHashCodeEx3
{
    public static void main(String[] args) {
        String s1 = "tutorialandexample.com";
        String s2 = "tutorialandexample.com";
        if((s1.hashCode())==(s2.hashCode())){
            System.out.println("welcome to tutorialandexample.com by Roy Nitish Nk");
        }else{
            System.out.println("Tutorial by Nk");
        }
    }
}

Output:

welcome to tutorialandexample.com by Roy Nitish Nk

Java String hashCode() example 4:

public class JavaStringHashCodeEx4
 {
    public static void main(String[] args) {
        String s1 = "tutorialandexample.com";
        String s2 = "tutorialandexample.com";       
        if((s1.hashCode())==(s2.hashCode())){
            int i,j,k;
         for(i=1; i<=5; i++)
          {
              for(j=4; j>=i; j--)
              {
                  System.out.print(" ");
              }
                    for(k=1; k<=(2*i-1); k++)
                       {
System.out.print("Nitish");//comment this for print star
System.out.print("*");
}
System.out.println("");
}
        }else{
            System.out.println("Tutorial by Nk");
        }
    }
}

Output:

Nitish*
Nitish*Nitish*Nitish*
Nitish*Nitish*Nitish*Nitish*Nitish*
Nitish*Nitish*Nitish*Nitish*Nitish*Nitish*Nitish*
Nitish*Nitish*Nitish*Nitish*Nitish*Nitish*Nitish*Nitish*Nitish*

Related Topics

Davis Staircase Problem in Java

Davis has several stairs in his home and prefers to ascend one, two, or three steps at a time. As a highly clever youngster, he thinks about how many ways...

3 minutes read.

Java Math addExact() Method

The addExact() method of Math class returns the sum of the two arguments, throwing an exception if the result overflows a long or an int. Syntax public static int addExact (int x,...

1 minute read.

Java 8 Consumer Interface in Java

The Consumer Interface is used to implement the functional programming in Java. The Consumer Interface indicates a function that accepts a single input and outputs a result. These functions don’t...

2 minutes read.

How to compare two dates in different format in Java?

We need to compare two dates frequently when coding. Real-world examples include sorting a list of persons by age or keeping track of students' attendance. To compare two dates, we...

7 minutes read.

Difference Between replace() and replaceall() in Java

In this article, you will be acknowledged about the replace() and replaceall() methods in java. Also, most importantly you will be acknowledged the differences between them along with the example...

4 minutes read.

How to Create an Immutable Class in Java

How to Create an Immutable Class in Java In Java, immutable means something that cannot be change. A class is called an immutable class if its content cannot be changed, once...

3 minutes read.

Palindrome Partitioning problem in Java

In this article, you will be acknowledged about partitioning of the palindrome. It can be done in many ways. This article makes sure every method is discussed. Palindrome If a string remains...

6 minutes read.

Java FileNotFoundException

FileNotFoundException is another exception class accessible in the java.io bundle. The exemption happens when we attempt to get to that document which isn't accessible in the framework. It is a checked...

5 minutes read.

Menu Driven Program in Java

Menu Driven Program in Java The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed...

3 minutes read.

Java URL Class with Example

Java URL Uniform Resource Locator To find any resource on the internet, you need to have an address of it. The URL and IP addresses are the pointers used for this purpose....

12 minutes read.

Java Integer toOctalString() method

The toOctalString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 8. Syntax public static String toOctalString (int  i) Parameters The parameter ‘i’ represents...

1 minute read.

Java Math.multiplyExact() method in Java

Java has an inbuilt math function called Math.multiplyExact() that returns the sum of the parameters. If the result exceeds an integer, an exception is thrown. There is no need to...

2 minutes read.

Java Map Example

In Java, the Map is an interface that is used mainly to denote key and value pairs. The central concept and theme of this mapping in the java collection framework...

4 minutes read.

Shopping Bill in Java

Java Shopping Bill Here in this program, we are about to create a JAVA class called Products which will have some properties or attributes like prod_name (Product Name ), qty (quantity),...

4 minutes read.

Java Full Stack

A person who can create both the front end and back end of an application is a full-stack developer. In essence, the term "Java full-stack" refers to a web developer...

9 minutes read.

Sylvester Sequence in Java

A Sylvester Sequence is a series of numbers in which each term is the sum of the terms before it plus 1. The sequence's first two terms are 2 and...

3 minutes read.

Collections in Java

Collection framework is one of Java's most powerful subsystems. It is a set of classes and interfaces that is all-the-rage technology for superintending objects and a group of objects. In...

6 minutes read.

Java Custom Exception

Java Custom Exception Java language facilitates us to create our own exceptions. The class intended to throw the Custom Exception has to be derived from the Java Exceptionor RuntimeExceptionclass. The Java...

4 minutes read.

How to Iterate List in Java?

List is a Collection foundation interface in Java. It enables us to keep the collection of objects in order. The four classes that make up the List interface implementation are...

3 minutes read.

Java Exception Propagation

Java Exception Propagation When an exception is being thrown from the peak of the stack and not getting caught, it runs down the stack to the previous method, which is sitting...

3 minutes read.