0

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
Cœur
38.9k25 gold badges206 silver badges281 bronze badges
asked Apr 13, 2015 at 15:53
2
  • "Hashcodes are unique values of each object" => nope... docs.oracle.com/javase/8/docs/api/java/lang/… Commented Apr 13, 2015 at 15:56
  • If it were as @Albertkaruna said, you could simply use == since the memory adress IS unique to each object. Commented Apr 13, 2015 at 16:34

3 Answers 3

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 ==.

answered Apr 13, 2015 at 15:59
2
  • 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 1311053135 Commented Apr 13, 2015 at 17:24
  • @Albertkaruna: Yes, because your Sample class probably overrides neither equals nor hashCode 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. Commented Apr 13, 2015 at 17:47
0

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

http://en.wikipedia.org/wiki/Java_hashCode%28%29

answered Apr 13, 2015 at 15:56
1
  • 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/… Commented Apr 13, 2015 at 15:58
0

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.

answered Apr 13, 2015 at 15:57
2
  • ControlAltDel did not answer the question at all but rather restated a part of it... Commented 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 information Commented Apr 13, 2015 at 16:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.