×

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.

Deadlock Prevention and avoidance in Java

This tutorial will discuss deadlock prevention and Avoidance in Java programming language. Introduction Multithreading in Java includes deadlock. We can multitask by running several threads concurrently in the multithreading environment. Deadlock occurs...

4 minutes read.

Switch Case Program in Java

Switch Case Program in Java The switch case program in Java controls which code snippet gets executed on the basis of the value of the expression mentioned in the switch statement....

22 minutes read.

Java Integer sum() method

The sum() method of Java Integer class add the two specified integers values. It returns the same result as given by + operator. Syntax public static int sum (int a, int b)  Parameters The...

1 minute read.

Java Math toDegrees() Method

The toDegrees() method of Java Math class converts a radian angle to an approximately equivalent angle measured in degrees. Syntax: public static double toDegrees(double angrad) Parameters: The parameter ‘angrad ‘represents an angle measured in...

2 minutes read.

Order of Execution of Constructors in Java Inheritance

Constructor in Java There are several distinctions between a method and a function object() in Java. The name of the function object()  is identical to the class name. There is no...

4 minutes read.

Java class class

Java Class class instances are an executing Java application's implementation of the classes and interfaces. As well as, every Array is indeed an object that is common for all Arrays...

6 minutes read.

How to Create an API in Java?

Introduction The API can be abbreviated as Application Programming Interface. An API is a combination of set of classes and interfaces. It is also equivalent to a simple java program. To...

12 minutes read.

Composition in Java

Composition Java uses the composition method to implement a has-a connection. Composition allows us to reuse code in the same way that Java inheritance does. The "is-a" relationship is implemented using the...

3 minutes read.

Stack vs Heap in Java

In Java, whenever we declare an object or create a variable, whether it is an instance variable, local variable, or static variable, a certain memory is used to store the...

4 minutes read.

Highest precedence in Java

In Java, the operator is the first thing that springs to mind when discussing precedence. The order in which the operators in an expression are evaluated is controlled by a...

3 minutes read.

Java Database Connectivity with MySQL

In this tutorial, we will learn how to connect Database with MySQL in Java. 5 Steps to Connect to the Database in Java Load the driver (or) Register the driver classEstablish a...

4 minutes read.

Java New Keyword

To create a class instance in Java, use the new keyword. In other words, it returns a reference to the memory that was allocated for a new object and instantiates...

3 minutes read.

Java Pi

What is Pi? There are many formulas in geometry that employ the Pi constant to calculate things like circumference, area, and volume. A circle's circumference divided by its diameter yields a...

3 minutes read.

How to remove special characters from String in Java

Strings in Java are Objects that are supported inside by a burn exhibit. Since exhibits are immutable (cannot develop), Strings are changeless too. A completely new String is made whenever...

5 minutes read.

Packages Program in java

Let us know what package is in the Java programming language, and later we will learn about types of packages and how to define a package. How to import Java...

4 minutes read.

Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers. Tetranacci number Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends...

3 minutes read.

How to get ASCII value of char in Java

Introduction: On this application, you will learn how to find and show the ASCII value of char in Java. That is done with the use of type-casting and also everyday...

4 minutes read.

Shallow copy in Java

Java's most important task is making a copy or clone of an object. In this part, we'll talk about shallow copies in Java and how to make them of Java...

4 minutes read.

Method Overloading In Java

If the same class consists of different methods with the same name and the methods can vary by different number of parameters then it is known as Method Overloading. Method...

2 minutes read.