Consider the piece of code given below. I was wondering how can a string present in string pool "s1" or "s2" have the same hashCode as a string present in heap as "s3" but outside string pool.
class Test{
public static void main(String[] args) {
String st1 = "Shiva";
String st2 = "Shiva";
String st3 = new String("Shiva");
System.out.println(st1 == st2);
System.out.println(st1 == st3);
System.out.println(st2 == st3);
System.out.println(st1.hashCode());
System.out.println(st2.hashCode());
System.out.println(st3.hashCode());
}
}
input: deep (master *) LanguagePackageInJava $ javac Lecture3.java
output: deep (master *) LanguagePackageInJava $ java Test
true
false
false
79855167
79855167
79855167
I have searched a lot regarding this question. Please do tell me where am I wrong in my thought process.