Reputation: 6096
I have read articles about object identity in object oriented context.Which says "Every object you create has its own unique identity". But I got confused by below code.
String str="Hello";
String str1="Hello";
System.out.println(str.hashCode()); //69609650
System.out.println(str1.hashCode()); //69609650
System.out.println(System.identityHashCode(str));//19313225
System.out.println(System.identityHashCode(str1));//19313225
hash code and identityhashcode for both str and str1 are same. Please correct me if I understood wrong.
Also what is difference between hashcode() and system.identityhashcode()
Upvotes: 1
Views: 128
Reputation: 36304
You are not using String objects, you are using String literals.. There is a very big difference between them. In your case, str and str1 both are pointing to the same "Hello" object in String pool. Try doing something like this :
StringBuilder sb1= new StringBuilder("Hello");
StringBuilder sb2= new StringBuilder("Hello");
System.out.println(sb1.hashCode());
System.out.println(sb2.hashCode());
System.out.println(System.identityHashCode(sb1));
System.out.println(System.identityHashCode(sb2));
output : 7214088 15020296 7214088 15020296
As StringBuilder creates new objects, you can see that the hashCodefor sb1 and sb2 are different. Which means they are not pointing to the same object. But str1 and str2 are pointing to the same String object. Read somthing about String Pools and permgen space for further information.
Upvotes: 0
Reputation: 425318
Both your objects are interned string constants. The JVM ensures the are the same objects.
The difference between hashCode and identityHashCode is hashCode is a hash of the balue of the string object, whereas the identityHashCode is a hash of the ibject's internal identifier.
Upvotes: 0
Reputation: 1567
Note that if you declare
String s2 = new String("Hello"),
System.identityHashCode(s2);will return different hashcode. This is because when declaration is like
String s = "something", jvm checks string pool to find out if there is identical literal. When declaration is like
String s = new String("something");jvm always creates a new object.
Upvotes: 1
Reputation: 1075467
What you're seeing is because you're using String
, which has a very special (and nearly unique) behavior: Your two strings are actually one String
object, because string literals are automatically intern
'd. The JDK and JVM work together to put string literals into a pool of String
instances which are reused, rather than creating separate String
instances for the same sequence of characters.
Try your experiment with new Object()
instead:
Object a = new Object();
Object b = new Object();
System.out.println(a.hashCode());
System.out.println(b.hashCode());
System.out.println(System.identityHashCode(a));
System.out.println(System.identityHashCode(b));
Also what is difference between hashcode() and system.identityhashcode()
The hashCode
function can be overridden by a class to return something appropriate for that class. System.identityHashCode
returns the same hashCode
that Object#hashCode
would have returned if the subclass hadn't overridden it.
So for Object
, you'd get the same return value from each of them. But for any class that overrides hashCode
to return something more appropriate for that class (which includes String
), you'd get different values.
Upvotes: 8
Reputation: 1948
As said in the documentation (http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#identityHashCode(java.lang.Object)), identityHashCode gives the same HashCode, wether or not the hashCode method was overriden.
Upvotes: 0
Reputation: 412
In addition to the previous answer, you can read a discussion around your second question here: How do hashCode() and identityHashCode() work at the back end?
Upvotes: 0