1

What is the logic behind == returning false when two reference variables are referring to same Object having same hash code values?

public class One {  
    public static void main(String[] args) {        
        One o = new One();
        One o1 = o; 
        System.out.println(o.toString());                                       
        System.out.println(o1.toString());                                      
        System.out.println(o.hashCode());                                       
        System.out.println(o1.hashCode());                                      
        
        // Why does it print false ?
        System.out.println(o.toString()==o1.toString());    // false                    
        System.out.println(o.hashCode()==o1.hashCode());    // true                 
        System.out.println(o.equals(o1));                   // true
                            
        System.out.println(o.toString().hashCode()==o.toString().hashCode());   // true 
    }
}

2 Answers2

2

The line with

System.out.println(o.toString()==o1.toString());    // false  

has a toString(). Each toString() creates a new String Object, which are different Objects that have their own memory locations. So the == actually checks the memory addresses between those new String Objects.

Always compare strings with String#equals, not with ==.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Andrew Wei
  • 870
  • 1
  • 7
  • 12
  • 2
    Good Answer. You might want to add a mention of `hashCode` returning an `int`, which as a primitive has no issue with comparing reference versus comparing value. In contrast, `toString` returning a `String` involves references as `String` is an object, not a primitive. In other words, explain why `o.hashCode()==o1.hashCode()` is a non-issue whereas `o.toString()==o1.toString()` is a problem. – Basil Bourque Aug 24 '21 at 07:05
  • @Basil Bourque I am beginner, please excuse me. Printing RV of o and o1 leads to same output, which is Fully Qualified Name @ hashCode value of the Object in HEX format. When o and o1, prints same output, how is it false ? – missing.hashcode Aug 24 '21 at 08:45
  • 1
    The contents of the string are the same, but each toString() generates a new memory location for the string. Then since Strings are an object, using `==` compares the references (memory locations) rather than the contents of the string. – Andrew Wei Aug 24 '21 at 08:57
1

== sign checks the memory addresses of objects being compared. In your case, toString() method creates two different String objects stored in two different places. That's why you get false when you try to compare them.
On the other hand, equals() checks the equality of contents of the objects. For your own data types, you should override this method to use it.
hashCode is some sort of a unique identification number for the contents of the objects. That is to say, objects that are equal to each other must return the same hashCode.

Muhteva
  • 2,729
  • 2
  • 9
  • 21