What is a clear comparison between two objects of the same class when using the == operator at the jvm level?

please excuse me if this is an existing stack overflow issue, but I would go through so many threads. But still I can’t understand what they are trying to discuss about two link comparisons of the same class, please help get 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");
     }
   }

Exit: Different objects of the same class are not equals

Now what I can’t understand is on what grounds the JVM will check these two objects (object1 and object2). And I would override the .equal (), hashCode (), toString () methods in the class A. See here my common 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 an explanation, I am very grateful to them.

+4
source share
7

, ==, equals() ( hashCode() toString()).

+1
== 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
0

java ==, , . , A, , . , ==.

java, equals().

if (object1 == object2)

if (object1.equals(object2))

, equals() A.

0

- .

== - : .

.equals() - , .

UPD

, 1) 2) 3) . , == , equals() - . Java, .

0

Object.equals()

/**
 * (...)
 * <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);
}

,

, equals(), .

0

JVM i.e abject. "==" , , , "" , .

0

== ( ), equals() ( ).

:

a == b a, b.

a != b, a.equals(b), a b - , . . a Set, b , .

toString() .

hashcode() , - () : !a.equals(b) && a.hashcode() == b.hashcode() . , equals(), hashcode.

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

0
source

Source: https://habr.com/ru/post/1527103/


All Articles