Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Java String hashCode()

Java String hashCode() returns the hashcode for the String. The hash value is used in hashing-based collections like HashMap, HashTable etc.

Lokesh Gupta

June 25, 2023

Java String class
Java Hashcode, Java String
Java String

Java String hashCode() method returns the hashcode for the String. The hashcode value is used in hashing-based collections like HashMap, HashTable etc. The hashCode() method must be overridden in every class that overrides equals() method to avoid any unpredicted behavior when used in hash-based collections.

Read More: Contract between hashCode() and equals()

1. String.hashCode() API

The syntax of the hashCode() API is as follows. It does not accept any argument and returns an integer representing the hash code value for this object.

public int hashCode();

The hashcode for a String object is computed as:

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

where :

  • s[i] – is the ith character of the string
  • n – is the length of the string, and
  • ^ – indicates exponentiation

The String’s hashCode() overrides the Object.hashCode() method. This method returns the hashcode as an Integer value.

2. String hashCode() Example

Java program for how to calculate the hashcode of string. In the given example, we are calculating the hashcode of two different strings, and both produce different hashcodes.

System.out.println( "howtodoinjava.com".hashCode() );

System.out.println( "hello world".hashCode() );

Program output.

1894145264
1794106052

Happy Learning !!

Comments

Subscribe
Notify of
2 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

String Examples

  • String Constant Pool
  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • Align Left, Right, Center
  • Immutable Strings
  • StringJoiner
  • Split a string
  • Escape HTML
  • Unescape HTML
  • Convert to title case
  • Find duplicate words
  • Left pad a string
  • Right pad a string
  • Reverse recursively
  • Leading whitespaces
  • Remove whitespaces
  • Reverse words
  • Find duplicate characters
  • Get first 4 characters
  • Get last 4 characters
  • (123) 456-6789 Pattern
  • Interview Questions

String Methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Java String endsWith()

Next

Java String indexOf()

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap