0

Possible Duplicate:
String equality vs equality of location

public class AboutStrings{
public static void main(String args[]){
String s1="hello";
String s2="hel";
String s3="lo";
String s4=s2+s3;

//to know the hash codes of s1,s4.

System.out.println(s1.hashCode());
System.out.println(s4.hashCode());

// these two s1 and s4 are having same hashcodes.

if(s1==s4){
System.out.println("s1 and s4 are same.");
}else
System.out.println("s1 and s4 are not same.");
}
}

In the above example even though s1 and s4 are refering to same object(having same hash codes),

it is printing s1 and s4 are not same.

Can anybody explain in detail why it is behaving like this?

Community
  • 1
  • 1
Vivek
  • 3
  • 2

6 Answers6

7

Just because two objects have the same hash code does not mean they are the same object (you are checking with == the object identity!).

You may want to call

s1.equals(s4)

instead - but even then, both could have the same hash code without being equal either: two objects that are equal must have the same hash code (to work properly in Collections etc), but not vice versa.

Roland Ewald
  • 4,630
  • 3
  • 35
  • 49
2

By using ==, you are checking to see if the two references to the Strings are the same.

What you want to do is check to make sure that the two objects are equal.

Do it like this:

if(s1.equals(s4)){
    System.out.println("s1 and s4 are same.");
} else {
    System.out.println("s1 and s4 are not same.");
}

Just because two Objects have the same hashcode, doesn't mean they are the same object.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
0

== checks if the two objects are the same, not if their content is the same. To compare the content of two objects, use .equals. Neither == nor equals use the hashcode.

if(s1.equals(s4)){
  System.out.println("s1 and s4 are same."); 
}else{
  System.out.println("s1 and s4 are not same."); 
}
Marius
  • 57,995
  • 32
  • 132
  • 151
0

The reason is that the statement String s1="hello"; create a new object and the statement String s4=s2+s3; create a completely new object. Hence thier hashcodes are different, though they have same characters. You miay want to use .equals() method instead!!

Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
0

See the following items in Effective Java: Second Editions by Joshua Bloch:

  • Item 8: Obey the general contract when overriding equals
  • Item 9: Always override hashCode when you override equals

From these items, everything should be more clear. Although there is no language feature to enforce this, objects that are equal should have the same hash code. Objects with the same hash code are not necessarily equal. And certainly, objects with the same hash code do not need to be the same.

Tom
  • 21,468
  • 6
  • 39
  • 44
0

If you intern your strings, you can use reference comparisons. Reference comparisons are much faster than invoking the equals method, though generally the total time spent doing either is negligible.

brianegge
  • 29,240
  • 13
  • 74
  • 99