×

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

Functional Interfaces in Java

Java has forever remained an Object-Oriented Programming language. By object-oriented programming language, we can declare that everything present in the Java programming language rotates throughout the Objects, except for some...

11 minutes read.

Rotate matrix by 90 degrees in Java | Rotate matrix in Java clockwise and anti-clockwise

In this article, you will be acknowledged about what is a matrix along with an example. Most importantly you will be equipped knowledge on how to rotate the matrix by...

4 minutes read.

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.

Java String format() method

format() method returns a formatted String based on the given locale,specified format and arguments. Syntax: public static String format(String format , Object… args) Parameter: locale : It specifies locale value to be applied on...

2 minutes read.

Java Image

For all other classes used for representing graphical images, Java's Image class serves as an abstract superclass. For images in Java, a specific form of object known as a BufferedImage...

4 minutes read.

New Features of Java 14

 Features like Switch expressions and text blocks which are previewed in Java 13 version are standardized in Java 14. Features in Java 14 Switch ExpressionText BlocksRecordsNull Pointer ExceptionsInstance ofPackaging ToolGarbage collectors 1.Switch Expressions Switch...

3 minutes read.

Java Math exp() Method

The exp() method of Math class returns Euler’s number(e) raised to the power of a double value. Syntax: public static double exp(double a) Parameters: The parameter ‘a’ represents the exponent e. Return Value: The exp ()...

2 minutes read.

Prime Number Program in Java

Prime Number Program in Java using for loop A natural number which is greater than 1 and has only two factors the number itself and 1 is called prime number. In...

2 minutes read.

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or...

4 minutes read.

Java Read File to String

There are different ways to deal with forming and examining a text record. This is normal while dealing with various applications. There are different ways to deal with looking at a...

6 minutes read.

Java Math with Methods and Examples

Java Math class contains various methods for performing math operations like min(), max(), avg() and various trigonometric functions like sin(), cos(), tan() etc. Methods: The java.lang.Math class contains various methods for performing...

5 minutes read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

5 minutes read.

Access modifiers in Java

Access modifiers in Java with Example In Java, there are two types of access modifiers one is a non-access modifier, and other is access modifier. If we talk about access modifier, there...

3 minutes read.

Best Java Security Framework

The security of applications is currently our top concern when creating them. The applications or bits of code running over the network are exposed to dangers and may jeopardize integrity,...

3 minutes read.

How Many Ways to Create Objects in Java

Introduction Java comes under the category of object-oriented programming languages. Since it is object-oriented, everything in Java is considered an object. Java is a diverse programming language that is designed to...

6 minutes read.

Java finalize()

Java finalize() In Java, the Object class is the root class that is inherited by all the Java classes. The class provides the finalize() method that is called just before the...

4 minutes read.

public static void main string args meaning in java

In java main() method is the initial point for execution of the program. If a program doesn’t contain the main method, the program will not execute. JVM(java virtual machine) starts...

3 minutes read.

Singleton class in Java

What is Singleton class in Java? Singleton means it is one. That means we can create only one instance or object of a class. For example, let us have a class...

3 minutes read.

Types of Garbage Collector in Java

Garbage collection is a Java feature that offers automatic memory management. The JVM is in charge of it. The programmer does not have to handle object creation and deallocation. We...

3 minutes read.

Repdigit Numbers in Java

A combination of repeated and digit, the term is. A repdigit, also known as a monodigit or a positional number system, is a natural number in recreational mathematics made up...

3 minutes read.