Q:

what is hash code and how it same for two string and with new string it will give diffrent output?

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());
    }
}
}

A:

No. == compares the references. this means == compares where in the memory the object is saved. hashCode() creates an integer from the attributes of an Object (the algorithm for this operation may vary from class to class). Comparison of objects should be done via o1.equals(o2).

