-3

Just curious about how does "==" work in Java? I create two classes ClassObject1 and ClassObject2, both implement interface I_Class Inside ClassObject1 and ClassObject2, I override the hashCode() and equals() functions, to make sure these two functions will return exactly same value for both these two classes.

public interface I_Class {

    public String getName();
}

public class ClassObject1 implements I_Class{

    private String name="";

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ClassObject1 other = (ClassObject1) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

    @Override
    public String getName() {
        return this.name;
    }
}
public class ClassObject2 implements I_Class{

    private String name="";

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ClassObject2 other = (ClassObject2) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

    @Override
    public String getName() {
        return this.name;
    }
}

I assume in this case

I_Class obj1 = new ClassObject1();
I_Class obj2 = new ClassObject2();
System.out.println(obj1 == obj2); // I assume here will write true

"==" compares the memory address, doesn't it?

yjasrc
  • 503
  • 1
  • 4
  • 15

4 Answers4

2

obj1.hashCode() == obj2.hashCode() doesn't mean that obj1 == obj2. Two objects can have the same hashCodes, no issues in that. But ultimately those are 2 different objects, and they can't be judged same.

Technically, in Java you should not assume any lower-level details of an object. You don't get to know the exact memory address of an object. That is how it is in Java. It is not similar to what you can do in C.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
1

Nope, It's will print false. == operator check if both references are referring the same object or not. In your case, both are two different objects.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
0

It will print false since obj1 and obj2 have different addresses.

See this discussion on strings for example.

Behavior regarding autoboxing is quite tricky, however.

Community
  • 1
  • 1
djechlin
  • 59,258
  • 35
  • 162
  • 290
0

I haven't carefully studied the code, but instead of

System.out.println(obj1 == obj2); // I assume here will write true

use

System.out.println(obj1.equals(obj2));

As you said in your OP, I think == compares memory addresses, while .equals() will use the equals method you've written.

Good luck!

Roy
  • 974
  • 6
  • 11