Albertkaruna
Albertkaruna

Reputation: 85

Why string class creates hashcode based on given string values?

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

Upvotes: 0

Views: 247

Answers (3)

Marvin
Marvin

Reputation: 14255

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

Upvotes: 3

that-ru551an-guy
that-ru551an-guy

Reputation: 300

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.

Upvotes: 0

ControlAltDel
ControlAltDel

Reputation: 35011

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

Upvotes: 0

Related Questions