Hashcodes are unique values of each object but why only String class creates hashcode like this.
public class StringSample
{
public static void main(String[] args)
{
String s1=new String("Example");
String s2=new String("Example");
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
}
}
Result
341682506
341682506
But this program gives different hashcode value
class Sample
{
Sample(String str)
{
}
}
public class Mainclass
{
public static void main(String[] args)
{
Sample s1=new Sample("Example");
Sample s2=new Sample("Example");
System.out.println(s1.hashCode());
System.out.println(s2.hashCode());
}
}
Result:
2018699554
1311053135
3 Answers 3
Your assumption is incorrect, cf. http://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--:
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.
However, if two objects are equal according to the equals(java.lang.Object)
method, they must have the same hash code:
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
In your example, s1
is equal to s2
according to the equals(java.lang.Object)
method, hence the hash code cannot be different without violating its general contract:
public static void main(String[] args) {
String s1=new String("Example");
String s2=new String("Example");
System.out.println(s1.equals(s2)); // true
}
Update:
Regarding your update and to maybe clarify my initial answer. Different from String
your Sample
class probably overrides neither hashCode()
nor equals(java.lang.Object)
. This means, that the default implementation of Java will be used, which works with the memory location of the current instance (I don't know the exact details, but that should indeed produce different hash codes for different objects). However, this would also imply that s1.equals(s2)
would be false
for both Sample
objects in your example.
Now why would String
override equals(java.lang.Object)
? Because one would expect that two strings with the same values (i.e. "Example") should be seen as equal by the code. To comply with the general contract between equals(java.lang.Object)
and hashCode()
(cf. the javadocs from above), String
now has to also override the hashCode()
method to produce equal hash codes for same values.
Simplified: If String
would not override hashCode()
to produce the observed output, two different strings could not be equal.
Disclaimer: Note that strings in Java are usually interned and behave slightly different than "regular" objects, but that only for the sake of completeness and further reading. You might also want to read about the differences between equals
and ==
.
-
Then this program gives different hashcode value class Sample { Sample(String str) { } } public class Mainclass { public static void main(String[] args) { Sample s1=new Sample("Example"); Sample s2=new Sample("Example"); System.out.println(s1.hashCode()); System.out.println(s2.hashCode()); } } Resule: 2018699554 1311053135Albertkaruna– Albertkaruna2015年04月13日 17:24:28 +00:00Commented Apr 13, 2015 at 17:24
-
@Albertkaruna: Yes, because your
Sample
class probably overrides neitherequals
norhashCode
and hence the hash code is Java's default, which should indeed be somewhat unique for each instance (I don't know the exact details on that). I will edit my question and try to include that part.Marvin– Marvin2015年04月13日 17:47:17 +00:00Commented Apr 13, 2015 at 17:47
The idea behind hashCode() is that if two objects are equal, they should share the same hashCode. That way, a HashMap or any other data structure can more quickly find and compare whether two Objects are equal
-
ControlAtDel is right that's why you need to implement hashcode + code in order to do not break the Object contract.The String hashcode implemantation gives you the anserw also: grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/…user1869209– user18692092015年04月13日 15:58:44 +00:00Commented Apr 13, 2015 at 15:58
This is because Strings are a Java Class, within them is a method called hashCode() taking no args hence ().
The hashCode method returns the string object's hashcode value.
Meanwhile, everything that does not give that output does not have that method or is not an object but a primitive data type.
-
ControlAltDel did not answer the question at all but rather restated a part of it...that-ru551an-guy– that-ru551an-guy2015年04月13日 15:59:11 +00:00Commented Apr 13, 2015 at 15:59
-
@Marvin - You did not answer his question either... Doesn't anybody on StackOverflow read the posts? or just a couple words and then spew a paragraph on related informationthat-ru551an-guy– that-ru551an-guy2015年04月13日 16:02:00 +00:00Commented Apr 13, 2015 at 16:02
==
since the memory adress IS unique to each object.