-4
    String obj1 = new String("abc");
    String obj2 = new String("abc");

    if(obj1.hashCode() == obj2.hashCode())
        System.out.println("hashcode()");


    if(obj1==obj2))
        System.out.println("==");

it prints hashcode() ,but why it does not print == as hashcode of both objects are same ?

having same hashcode doesn't mean references are pointing to same object?

Dip Halani
  • 11
  • 7
  • 3
    What made you think that hash codes indicated reference equality? – Jon Skeet Mar 02 '17 at 14:11
  • The hashcode gets generated by the hashCode() method in your object. You can just look at the source code of the String class and have a look at how its hashCode() works. – OH GOD SPIDERS Mar 02 '17 at 14:12
  • i didn't mean that. == checks if both reference are pointing to same object or not, then having same hashcode doesn't mean objects are sharing same address? – Dip Halani Mar 02 '17 at 14:14
  • 4
    no, why should it? Override hashCode and return 0 - now EVERYTHING is the same object??? That would make absolutely no sense – luk2302 Mar 02 '17 at 14:14
  • The hashcode can be calculated in many different ways and even if it is equal that doesn't mean the objects are the same, just that the hashing function happened to return the same value. – Thomas Mar 02 '17 at 14:15
  • Note that the default `hashCode()` probably uses `System.identityHashCode(...)` which might be based on memory address but that's still no guarantee that equal hashes mean the same object, since for example the hashes are (32-bit) integers while memory addresses could be 64-bit, depending on the JVM. – Thomas Mar 02 '17 at 14:17
  • @luk2302 u r right. but what does that indicate if object are having same hash code? (i knw java maintains string constant pool) – Dip Halani Mar 02 '17 at 14:18
  • 2
    "what does that indicate if object are having same hash code" - exactly that and nothing more: they have the same hashcode. They _might_ be equal or the _might not_, that's why there's the `equals()` method: collections that use `hashCode()` need to call `equals()` as well and that's why those 2 methods are coupled (see their general contract as per the documentation). – Thomas Mar 02 '17 at 14:19
  • @Thomas yes,your answer makes sense. – Dip Halani Mar 02 '17 at 14:19
  • can anybody tell me what advantage can we take by overriding hashcode method? – Dip Halani Mar 02 '17 at 14:21
  • Have a look at the documentation of `hashCode()` and `equals()` - if you override one you should override the other as well. Basically you'll eventually want to override `equals()`, e.g. to get `new Long(1).equals( new Long(1)) == true` and since both methods are linked you should override `hashCode()` so that both instances return the same hashcode (if they are _logically_ equal). – Thomas Mar 02 '17 at 14:24
  • thank you @thomas. – Dip Halani Mar 02 '17 at 14:30

2 Answers2

0

The same hashCode does not mean the same object, take a look at the general contract of hashcode

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • 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.
  • 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, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Dazak
  • 1,011
  • 2
  • 9
  • 17
0

obj1 == obj2 will return false because "==" compares references and not values, your objects both have same value but are stored in different memory parts.

.equals() compares values of your objects.

so:
obj1 == obj2 (false)
obj1 == obj1 (true)

hering
  • 1,956
  • 4
  • 28
  • 43
gmed
  • 140
  • 1
  • 10