Answer the question
In order to leave comments, you need to log in
Why do strings always have the same hashcode()?
String str1 = "Hello";
String str2 = "Hello";
Ob ob1 = new Ob("Object", 1);
Ob ob2 = new Ob("Object", 1);
Answer the question
In order to leave comments, you need to log in
Good afternoon.
Here is the answer to your question:
https://www.baeldung.com/java-string-immutable
String str1 = "Hello";
String str2 = "Hello";
Because:
1. In fact, they have the same references, because during compilation all string literals turn into constants.
2. This is necessary so that you can normally use strings as keys in the map.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question