What is the difference?
I thoght the reference of the object will returned by both of them , but it seems not.
public class Solution {
public static void main(String[] args) {
String s = "abcd";
String s1 = new String("abcd");
String s2 = new String("abcd");
String s3 = "abcd";
System.out.println(Integer.toHexString(System.identityHashCode(s)));
System.out.println(Integer.toHexString(System.identityHashCode(s1)));
System.out.println(Integer.toHexString(System.identityHashCode(s2)));
System.out.println(Integer.toHexString(System.identityHashCode(s3)));
System.out.println();
System.out.println(Integer.toHexString(s.hashCode()));
System.out.println(Integer.toHexString(s1.hashCode()));
System.out.println(Integer.toHexString(s2.hashCode()));
System.out.println(Integer.toHexString(s3.hashCode()));
}
}
Output:
7960847b // i think this is the reference of the object, memory-address
6a6824be // new object = new address. OK.
5c8da962 // new object = new address. OK.
7960847b // String pool. Same adress as the first, it's understandable. OK.
2d9442 // but why are these hashCodes the same? What kind of hashCodes are these??...
2d9442
2d9442
2d9442