GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — Java - String hashCode() Method

java package tutorial
  • Java.lang - Home
  • Java.lang - Boolean
  • Java.lang - Byte
  • Java.lang - Character
  • Java.lang - Character.Subset
  • Java.lang - Character.UnicodeBlock
  • Java.lang - Class
  • Java.lang - ClassLoader
  • Java.lang - Compiler
  • Java.lang - Double
  • Java.lang - Enum
  • Java.lang - Float
  • Java.lang - InheritableThreadLocal
  • Java.lang - Integer
  • Java.lang - Long
  • Java.lang - Math
  • Java.lang - Number
  • Java.lang - Object
  • Java.lang - Package
  • Java.lang - Process
  • Java.lang - ProcessBuilder
  • Java.lang - Runtime
  • Java.lang - RuntimePermission
  • Java.lang - SecurityManager
  • Java.lang - Short
  • Java.lang - StackTraceElement
  • Java.lang - StrictMath
  • Java.lang - String
  • Java.lang - StringBuffer
  • Java.lang - StringBuilder
  • Java.lang - System
  • Java.lang - Thread
  • Java.lang - ThreadGroup
  • Java.lang - ThreadLocal
  • Java.lang - Throwable
  • Java.lang - Void
  • Java.lang - Interfaces
  • Java.lang - Errors
  • Java.lang - Exceptions
  • Java.lang Package Useful Resources
  • Java.lang - Useful Resources
  • Java.lang - Discussion
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

Java - String hashCode() Method



Previous
Quiz
Next

Description

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.

Getting HashCode of Empty String 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.

package com.tutorialspoint;

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

Getting HashCode of a String 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.

package com.tutorialspoint;

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

Getting HashCode of Single Character String 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.

package com.tutorialspoint;

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
Tutorix - AI Tutor
Previous Quiz Next
Advertisements
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.