简体   繁体   中英

What is clear cut comparison between two objects of same class when using == operator per jvm level?

please apologize me if it is already existing question in stack overflow, but I would go through so many threads of stack overflow. But still I am unable to understand what they are try to discuss about two references comparison of the same class, please help to come out of this problem. This is my actual analysis

public class A {
  public static void main(String[] args) {
    A object1 = new A();
    A object2 = new A();
    if (object1 == object2)
       System.out.println("Different objects of the same class are equals");
    else
       System.out.println("Different objects of the same class are not equals");
     }
   }

Output : Different objects of the same class are not equals

Now what I am unable to understanding thing is on which bases JVM will check these two objects (object1 and object2). And I would override .equal(), hashCode(), toString() methods in A class. Please see here my total code.

public class A {

@Override
public int hashCode() {
    return 2000;
}

@Override
public String toString() {
    return "12345";
}

public static void main(String[] args) {
    A object1 = new A();
    A object2 = new A();
    if (object1 == object2)
       System.out.println("Different objects of the same class are equals");
    else
       System.out.println("Different objects of the same class are not equals");
   }
 }

Please give me clear cut explanation, I am very very thankful to them.

如果使用==比较它们,则两个不同的对象总是不同的,无论equals() (和hashCode()toString() )的实现是什么。

== check the reference of the Object.If you create two object of same class then reference will be different.There  will be `NO` difference whether you implement equal or hashcode function

When you compare two objects in java using ==, it's checking to see if the two objects are in the same location in memory. So when you create two instances of the A class, in your example, they may be the same, but they are both stored in different locations in memory. That's why it says they aren't equal when using ==.

If you want to compare two objects in java, you want to use the equals() method. So instead of using

if (object1 == object2)

you would use

if (object1.equals(object2))

Furthermore, to make this work, you would have to override the equals() method for the class A.

Class is a template to create many objects.

== is a facility to compare objects: same object or not.

.equals() is a facility to check if objects have the same state.

UPD

I mean in formal theory objects have 1) identity 2) state 3) behavior. So == is for checking the identity and equals() is for checking the state. It's not strictly so in Java but that might help to progress in understanding.

This is the Object.equals() method implementation

/**
 * (...)
 * <p>
 * The {@code equals} method for class {@code Object} implements
 * the most discriminating possible equivalence relation on objects;
 * that is, for any non-null reference values {@code x} and
 * {@code y}, this method returns {@code true} if and only
 * if {@code x} and {@code y} refer to the same object
 * ({@code x == y} has the value {@code true}).
 * <p>
 * Note that it is generally necessary to override the {@code hashCode}
 * method whenever this method is overridden, so as to maintain the
 * general contract for the {@code hashCode} method, which states
 * that equal objects must have equal hash codes.
 *
 * @param   obj   the reference object with which to compare.
 * @return  {@code true} if this object is the same as the obj
 *          argument; {@code false} otherwise.
 * @see     #hashCode()
 * @see     java.util.HashMap
 */
public boolean equals(Object obj) {
    return (this == obj);
}

can't be clearer than that

so if you don't override equals(), you're using this method.

In your scenario JVM will compare objects on the basis of reference ie according to the address of two abject. "==" will check whether two object have the same address or not which is clearly not the same as you are using "new" to create object which returns reference of newly created objects.

The == operator checks object identity (two variables refer to the same instance in memory), while equals() defines object equality (two variables represent the same object).

Example:

If a == b and you change a then you also change b .

If a != b but a.equals(b) then a and b are different instances but they are considered equal. Example: If a is in a Set , then this set can not contain b at the same time, because they are equal and sets don't contain duplicates.

The result of toString() is completely irrelevant for object identity or equality.

hashcode() must return the same value for equal instances, but can also return the same hashcode for different (unequal) instances: !a.equals(b) && a.hashcode() == b.hashcode() may be the case. So if you override equals() you have to override the default implementation of hashcode as well.

Your example shows 2x the keyword new . This means you have two instances in memory, so they are definitely not == , but may equal() each other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM