-1

A small program on String

    String str1 = new String("Hello");
    String str2 = "Hello";
    System.out.println("=======================");
    System.out.println("Srtr1 == Str2 :: "  + (str1 == str2));
    System.out.println("Srtr1.equals(Str2) :: " + str1.equals(str2));

Output: Srtr1 == Str2 :: false Srtr1.equals(Str2) :: true

Now how it is possible ? As we know that if equals() of two objects are true, then HashCode() of two objects must be true. But if HashCode() is true, then Equals() may be true or may not be true.

But in the above program, we see that, equals() of two String objects are true, but their hashCode() is returning false.

why it is so ???

2 Answers2

1

You're not comparing the hashCode()s at all. You're comparing equality and identity.

The identity of the two Strings is different, because you have seen to it that new objects are allocated for each. The identity comparison '==' returns false.

The value of the two Strings is the same, and so the equals() method returns true.

scottb
  • 9,908
  • 3
  • 40
  • 56
  • I understand that System.out.println("Srtr1 == Str2 :: " + (str1 == str2)); will check the Identity of the two String objects. Now I want to know what does this '==' operator internally do ? On which criteria basis it will compare the two objects and return true/false ? – Santanu Das Mar 31 '15 at 02:23
  • For Java primitives, the `==` operator compares value only (primitives do not have any identity that is separate from their value). For reference types (such as objects and array objects). the `==` operator compares identity only. Identity may be interpreted to mean "memory address". If two object references point to the same physical address in memory, they are said to be identical. It follows that objects may have different identities (storage addresses), but still contain the same value. Identical objects always have the same value. – scottb Mar 31 '15 at 17:04
0

The == operator will verify if the pointer to each String is pointing to the same object, which in this case they are not. This is very different from the hashCode() of an object, which returns an integer hash of the data within that object. To verify this, you should replace

System.out.println("Srtr1 == Str2 :: "  + (str1 == str2));

by

System.out.println("Srtr1.hashCode() == Str2.hashCode() :: "
+ (str1.hashCode() == str2.hashCode()));

This will demonstrate that the hashCode of both your Strings are, indeed, equal :)

Jonathan Pitre
  • 2,355
  • 3
  • 22
  • 38
  • I understand that System.out.println("Srtr1 == Str2 :: " + (str1 == str2)); will check the Identity of the two String objects. Now I want to know what does this '==' operator internally do ? On which criteria basis it will compare the two objects and return true/false ? – Santanu Das Mar 31 '15 at 02:22
  • It checks to see if the references (str1 and str2) point to the same object in memory. In your above case, you have two Strings, both representing "hello", so they are .equals() to each other, but since they are two objects in memory == will give false. You created two String objects, but they happen to represent the same word. See http://stackoverflow.com/questions/7520432/java-vs-equals-confusion for more details. – Jonathan Pitre Mar 31 '15 at 16:20
  • I understand that you are saying as I have created two String objects, but their memory locations are different, then only I am getting "==" as False. Now I want to know which memory address it is checking, reference memory address in Stack OR string objects memory address in Heap ??? And one more doubt, then what hashCode() returns for any object, say for Strings object ? IS hashCode() returns the memory address of the object ? – Santanu Das Mar 31 '15 at 16:57