Java String hashCode() Method



The Java String hashCode() method is, used to retrieve the hash code for the current string. It returns an integer value that represents the value of the input object. In Java, a hash code is a numeric(integer) value that can be, used to identify an object during equality testing and also serve as an index for the object.

The hashCode() method does not accept any parameter. It does not throw any exception while retrieving the has code of the current string.

Note − A hash code value of an object can be changed in multiple executions of the same application.

Syntax

Following is the syntax of the Java String hashCode() method −

public int hashCode()

Parameters

  • It does not accept any parameter.

Return Value

This method returns a hash code value for this object.

Example

If the given string is empty, the hashCode() method returns zero as a hash code value.

In the following program, we are instantiating a string class with an empty value. Using the hashCode() method, we are trying to retrieve the hash code value of the current string.

import java.lang.*;
public class HashCode {
   public static void main(String[] args) {
      
      //instantiate the string class
      String str = new String();
      System.out.println("The given string is an empty." + str);
      
      // using hashCode() method
      int str_hash_code = str.hashCode();
      System.out.println("The hash code of the current string is: " + str_hash_code);
   }
}

Output

On executing the above program, it will produce the following result −

The given string is an empty.
The hash code of the current string is: 0

Example

If the given string is not empty, this method returns the hash code value of the current string.

In the following example, we create an object of the string class with the upper case value of "TUTORIX". Then, using the hashCode() method, we are, trying to retrieve the hash code value of the current string.

import java.lang.*;
public class HashCode {
   public static void main(String[] args) {
      
      //create an object of the string class
      String str = new String("TUTORIX");
      System.out.println("The given string is: " + str);
      
      // using hashCode() method
      int str_hash_code = str.hashCode();
      System.out.println("The hash code of the current string is: " + str_hash_code);
   }
}

Output

Following is the output of the above program −

The given string is: TUTORIX
The hash code of the current string is: -245613883

Example

Using the hashCode() method, we can retrieve the hash code of the single character string.

In this program, we are creating a string literal with the value ‘A’. Using the hashCode() method, we are trying to retrieve the hash code of the current sequence.

import java.lang.*;
public class HashCode {
   public static void main(String[] args) {
      
      //create string literal
      String str = "A";
      System.out.println("The given string is: " + str);
      
      // using hashCode() method
      int str_hash_code = str.hashCode();
      System.out.println("The hash code of the current string is: " + str_hash_code);
   }
}

Output

The above program, produces the following output −

The given string is: A
The hash code of the current string is: 65
java_lang_string.htm
Advertisements