-2
        String str1="hello";
        String str2="hell";
        String str3=str2+"o";

        System.out.println(str1==str3); // fasle
        System.out.println(str1.hashCode()); // 99162322
        System.out.println(str2.hashCode()); // 3198781
        System.out.println(str3.hashCode()); // 99162322

my question is why str1==str3 return false if there hashcode is same? i want to know internaly working of == operator and hashcode

Arun nagar
  • 178
  • 4
  • 13
  • 2
    If you ever, _ever,_ use `==` on two things whose types start with a capital letter, you are already doomed. Always, _always_, assume that `==` is out to get you. Always, always, use `.equals`. – Louis Wasserman Mar 30 '16 at 04:34
  • Different strings can have the same hash code.This is called a collision in hashing.http://eclipsesource.com/blogs/2012/09/04/the-3-things-you-should-know-about-hashcode/ – Arun Peddakotla Mar 30 '16 at 04:37
  • @LouisWasserman: can you tell how == works in java – Arun nagar Mar 30 '16 at 04:37
  • 1
    == checks if two things are EXACTLY the same object, not if they have the same content.http://stackoverflow.com/questions/4744953/string-comparison-with-logical-operator-in-java – Arun Peddakotla Mar 30 '16 at 04:39
  • @Arunnagar How `==` works is that it is out to get you. It is out to do whatever is least useful when you try to use it. That is, quite seriously, all you need to know about `==` in Java. – Louis Wasserman Mar 30 '16 at 04:48
  • `hashCode()` is a function in `String` Object, the [doc](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#hashCode()) tells how it should be implemented. – Nier Mar 30 '16 at 04:50
  • @LouisWasserman I think its a bit crude to say that you should *never* use `==`, however in most cases it is a good rule-of-thumb. – Obicere Mar 30 '16 at 05:00

3 Answers3

5

If we declare String str1="hello", The Object has been allocated in SCP(String Content Pool) but memory for str3 will be allocated in heap because of runtime operation(str3=str2+"o"). So, even hashcode is same contents of variables of str1 and str3 will be in different places. So, == always check for reference and in String class equals() check for content.

Mickey Patel
  • 501
  • 1
  • 4
  • 16
0

Instead of using "==" signs for conditional statement for string type, you should use ".equals"

try

System.out.println(str1.equals(str3));

for more information, refer to https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equals(java.lang.Object)

To explain "==" operator in Java, for string type, this compares the address of contents by "call by reference" while .equals compare contents of address.

For int, float, etc type using "==", it compares the value by "call by value"

Sean83
  • 493
  • 4
  • 14
0

The == operator is true only if two objects refer to the same instance .

The equas method compares the value and not the reference . In any object you can ovveride this method yourself and implement what quality means to your object. When overriding equlas method you should override hash code method as well. Hash code is used to speed up comparing object espessialy in arrays If hash code different in two objects then calling equals is not needed. However if hash code is the same it not 100% that objects are equal !

Rami Loiferman
  • 853
  • 1
  • 6
  • 22