STRING INSTANCE METHODS
Java String hashCode() method
hashCode() returns int number calculated from a String string's characters. It is most often used by collections such as HashMap and HashSetto find values faster.
Your first hashCode() call
String tekstas = "Java";
System.out.println(tekstas.hashCode());2301506
tekstasstores the value Java.hashCode()reads the characters and calculates an integer.- The result is
int, so it can be printed, stored in a variable or passed to another method. - The same text in the same Java version always returns the same
Stringhash code.
Method syntax
public int hashCode()
| Part | Meaning |
|---|---|
| Calling object | A specific String, for example, tekstas.hashCode() |
| Parameters | No parameters |
| Return type | int |
| The main rule | If two String strings are equal according to equals(), their hashCode() results must match |
| Important limitation | Equal hash codes do not necessarily mean equal strings |
Why is hashCode() an instance method?
hashCode() is called on a specific string because the number is calculated from that String object's characters:
int kodas = tekstas.hashCode();
We therefore do not write String.hashCode(). The method needs a specific String value whose characters are used to calculate the hash code.
Equal text has equal hash codes
String pirmas = "Java";
String antras = new String("Java");
System.out.println(pirmas.equals(antras));
System.out.println(pirmas.hashCode());
System.out.println(antras.hashCode());true
2301506
2301506
pirmasandantrasare two separateStringobjects.- Their text contents match: Java.
equals()returnstruebecause it compares text.- Since the strings are equal, both
hashCode()calls return the same number.
Different text usually has different hash codes
String pirmas = "Java";
String antras = "java";
String trecias = "Kava";
System.out.println(pirmas.hashCode());
System.out.println(antras.hashCode());
System.out.println(trecias.hashCode());2301506
3254818
2331297
Java, java and Kava are different strings, so their hash codes differ in this example. Uppercase and lowercase letters have different character values, so J and j produces a different calculation result.
Empty text and a space
String tuscias = "";
String tarpas = " ";
System.out.println(tuscias.hashCode());
System.out.println(tarpas.hashCode());0
32
A blank String has no characters, so its hash code is 0. A space is not nothing: it is a real character with value 32, so " " hash code differs from "".
How is a String hash code calculated?
String.hashCode() uses a formula where each character contributes to the result. For the text ABC , the simplified calculation is:
'A' * 31² + 'B' * 31¹ + 'C' * 31⁰
String tekstas = "ABC";
int rezultatas = tekstas.hashCode();
System.out.println(rezultatas);64578
The number 31 is the multiplier used in String hash calculation. We rarely calculate it manually, but it explains why character order matters: ABC and CBA are different strings and usually have different hash codes.
Equal hash codes do not mean equal text
String pirmas = "FB";
String antras = "Ea";
System.out.println(pirmas.hashCode());
System.out.println(antras.hashCode());
System.out.println(pirmas.equals(antras));2236
2236
false
FB and Ea are a classic small example where two different String strings have the same hash code. This is called a collision. Therefore, hashCode() is not a definitive equality test: use equals().
The connection to HashMap
import java.util.HashMap;
HashMap taskai = new HashMap<>();
taskai.put("Java", 10);
System.out.println(taskai.get("Java"));
System.out.println("Java".hashCode());10
2301506
When String is used as a HashMap key, Java uses its hashCode()to quickly choose where to look for the value. If several keys share a hash code, Java still checks actual equality using equals().
hashCode() is not a user-facing ID
hashCode()is not encryption and is not a secret value.- A hash code can be negative because the return type is
int. - Different strings can share a hash code.
- Do not display a hash code as a user number, security key or unique identifier.
- To check string equality, use
equals()rather than onlyhashCode().
The method cannot be called on null
String tekstas = null;
System.out.println(tekstas.hashCode());NullPointerException
hashCode() is an instance method. If a variable holds null, it refers to no String object, so Java has nothing on which to call the method and throws NullPointerException.
Safe null checking
String tekstas = null;
int kodas = tekstas == null ? 0 : tekstas.hashCode();
System.out.println(kodas);0
If the value may be null, check it first. This example uses the conditional operator: if tekstas == null, it selects 0, while hashCode() is not called.
Common mistakes
- Assuming that
hashCode()is always unique for each string. - Comparing only hash codes and forgetting
equals(). hashCode()is confused with a secure password-hashing algorithm.- Calling the method through a
nullvalue. - Expecting
"Java"and"java"will have the same hash code.
When should you use hashCode()?
In ordinary simple Java code, String.hashCode() is rarely called directly. It matters when understanding hash-based collections, why equals() and hashCode() must be consistent in classes, and how collections quickly find values by key.