×

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

Timestamp Operation in Java

JDBC escape syntax is supported by Timestamp's formatting and parsing functions. Additionally, it adds support for fractional seconds values for SQL TIMESTAMP.java.util.Date is wrapped in a lightweight wrapper that enables...

3 minutes read.

Split the Number String into Primes in Java

Given is a string that only contains digits and serves to represent a number. Our goal is to split the string of numbers in a way that ensures each segment...

2 minutes read.

Java Program to Add Digits Until the Number Becomes a Single Digit Number

We will develop Java programme that increase a number's digit count in order to reduce it to one. The issue is also known as the digit root problem. Example Consider the case where...

3 minutes read.

Difference between final, finally, and finalize()

Difference between final, finally, and finalize() In Java, final, finally and finalize sound similar they are totally different in functionality. Final and finally are the two keywords but the finalize is...

4 minutes read.

CRC Program in Java

The acronym CRC stands for Cyclic Redundancy Check. It is invented by W. Wesley Peterson in 1961. It is an error detection technique that detects errors in digital networks (also...

5 minutes read.

Java throw

Java throw Sometimes it is required in the code to throw an exception deliberately. In order to achieve the same, the Java throw keyword should be used. One can throw either...

3 minutes read.

Dining Philosophers problem in Java

The Problem of the Dining Philosophers illustrates a concurrency issue involving the distribution of scarce resources among conflicting processes. Problem Statement Imagine a dining table with a circle in the middle and five...

3 minutes read.

Zygodromes in Java

Zygodrome is a positive number created by the same digits running non-trivially. A number is called a zygodrome if identical digits constantly occur together (in pairs). The Greek word "zyg"...

4 minutes read.

Java Solid Principles

Java implements the object-oriented SOLID principles for the design of software architecture. Solid Principles Java implements the object-oriented SOLID principles for the design of software architecture.   Five guiding principles transformed...

4 minutes read.

Why String in Immutable in Java?

Why String in Immutable in Java Immutable means unchangeable or unmodifiable.  Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change...

2 minutes read.

Sealed Class in Java

What is a Sealed Class in Java? In programming, the two main issues that must be taken into account when creating an application are security and control flow. The use of...

5 minutes read.

Java ArraylistRemove() Time Complexity

In this tutorial, we will learn about how we can remove time complexity in Java ArrayList. But unless we will not learn what is ArrayList, we don’t understand this process....

7 minutes read.

Thread Concept in Java

What is a Thread? A subprocess that is lightweight in Java is known as a thread. It is the smallest unit of a process. For the running of multiple tasks parallelly...

3 minutes read.

Graph Problems in Java

A graph is a special type of data structure used in programming that is made up of edges connecting each set of finitely many nodes, or vertices, to each other....

14 minutes read.

Sleep Time in Java

Sleep is amethod in java that is related to thread class and it is a concept of multithreading and is used to stop the execution of the current thread a...

4 minutes read.

Java Create File

A File is a hypothetical way, which has no genuine presence. It is very much like while "using" that File that the major real activity of putting away something in...

5 minutes read.

Difference between JIT and JVM in Java

In this tutorial, we will discuss the difference between JIT (Just In Time Compiler) and JVM (Java Virtual Machine) in Java. Before we move to the differences, let's understand what...

4 minutes read.

Check the presence of Substring in a String in java

In java, the string can be treated as class and datatype. The string contains words and numbers but should be in double-quotes. Example: ” Omsairam” Substring The part of the string is called...

2 minutes read.

Transaction Management in java

Definition: A database application is an application that is running against a relational database and executes one or more transactions. A transaction is an executing program that contains some database operations,...

4 minutes read.

IdentityHashMap in Java

The IdentityHashMap class is comparable to the HashMap class and is an AbstractMap implementation. However, when comparing the key, it uses reference equality rather than object equality (or values). Identity HashMap...

6 minutes read.