I am using the same string as str1 and str2 when I compare this it will give true output with same hashcode. but when I use new String it will give different output with same hash code. As per my information == keyword compare string according to hash code but still I get different output?
public class StringClass {
public static void main(String args[]){
String str1="john";
String str2="john";
String str3=new String("john");
String str4=new String("john");
if(str1==str2){
System.out.println("Both string have same hash code");
System.out.println(str1.hashCode());
System.out.println(str2.hashCode());
}
if(str3==str4){
System.out.println("both have same hash code");
System.out.println(str3.hashCode());
System.out.println(str4.hashCode());
}
}
}