×

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

Java For Keyword

For as a keyword in java: When we need to run a set of statements repeatedly in Java, we use loops. The Java for loop offers a clear way to express...

4 minutes read.

Interface in Java

  Interface in Java In Java, the interface is just like a class that has only static constants and abstract methods. It is used to achieve polymorphism so that it can also...

6 minutes read.

Java Wrapper Class

Wrapper class in Java encapsulates a primitive type within an object. In other words, it is a unique mechanism of Java to convert primitive type in to object and object into primitive type....

3 minutes read.

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

Hollow Diamond Pattern in Java

Why are patterns important? Programmers frequently create Java pattern programs to practice coding and ace interviews. Interviewers frequently test candidates' logical reasoning and implementation by asking about pattern programs. Hollow Diamond Pattern The...

7 minutes read.

Set Matrix Zeros in Java

In coding round interviews, it is frequently asked as the most significant challenge. an m*n matrix is provided. Set the entire column and row of the matrix to 0 if any...

6 minutes read.

Java Lock

A lock is indeed a threaded synchronization technique similar to Java's synchronized blocks, however, locking can be more complex. It's not like we can completely get rid of the synchronized...

5 minutes read.

Java CountDownLatch

Another crucial classes for concurrent execution is CountDownLatch. It is a synchronisation tool that enables one or more threads to await until a series of tasks started by another thread...

4 minutes read.

How to Import Packages in Java

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Find Unique Elements in Array Java

An array in Java is a grouping of objects that share the same type of data. We are free to enter identical or repeating elements into an array. Therefore, we...

6 minutes read.

Java IO

It is a part of java libraries but is often known as I/O streams, file I/O, and file handling. The Java I/O concept satisfies the need for input processing and...

4 minutes read.

Java StringBuffer vs StringBuilder

Java StringBuffer vs StringBuilder StringBuffer The StringBuffer class is used to create mutable string. StringBuffer shows writable and growable character sequences. Java StringBuffer program FileName: StringBufferExample.java // A basic program that demonstrates the working...

2 minutes read.

String Matches in Java

Firstly we have to know something about String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java...

3 minutes read.

Convert milliseconds to date in Java

In Java, we frequently need to convert milliseconds into Dates with several formats, including dd MM yyyy and dd MM yyyy HH:mm:ss:SSS Z, among others. The Date class is one of the most...

4 minutes read.

House Numbers in Java

In this section, we will discuss about house number in Java. It is a sum of cubes, each of which has a dimension of h + 1. There is a...

3 minutes read.

Thread Program in Java

Thread Program in Java Thread program in Java is the continuation of multithreading program in Java. In this topic, we will learn about the usage of threads, race condition in multithreading,...

8 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.

Java Wrapper classes

Java Wrapper classes A wrapper class is a class whose object contains a primitive data type; moreover it provides a way to use primitive data type (int, boolean, etc.) as objects. Wrapper...

2 minutes read.

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.Constructor chaining is the process of calling one constructor from another constructor using the same object....

2 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.