When does a.equals (a) return false?

I was wondering in which cases a variable in java cannot be equal (using the method equals()) for itself. I am not talking about the object here, but the variable itself (as long as the code compiles and returns false when called equal). The only situation in which it works is what I have found so far:

public class A {
    public static void main(String args[]){
        A a = new A();
        System.out.println(a.equals((a = null)));
    }
}

Is there any other case in which it a.equals(a)returns false?

EDIT: redefinition is not allowed equals(), but you can change (distinguish, inherit) aas much as you want if the variable acompares itself at the end.

+4
source share
6 answers

false equals, equals :

class Test {
    public static final A a = new A();

    public static void main(String... args) throws Exception {
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    a.x += 1;
                }
            }
        }.start();
        Thread.sleep(10);

        System.out.println(a.equals(a));  // <---
    }
}

class A {
    int x;

    @Override
    public boolean equals(Object o) {
        return (o instanceof A) && ((A)o).x == x;
    }
}
false
+4

Object Oracle:

public boolean equals(Object obj)

, - "" .

equals :

It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
For any non-null reference value x, x.equals(null) should return false. 

equals Object ; x y true , x y (x == y true).

, hashCode , , hashCode, , -.

:   obj - , . :   true, obj; false .

,

false, a.equals(null); a b ( a b ) , .. a.equals(b) false.

, :

It is reflexive: for any non-null reference value x, x.equals(x) should return true.

, : x ( a ):

a.equals(a);

+2

khale Frakcool. , ,

System.out.println(a.equals((a = new A())));

, , false, .

+1

, , . , .

String foo = "";
bool a = foo.equals(foo); // Here true, the easy one
foo = "some value";
bool b = foo.equals(foo); // Here true, since it changed and then compared to itself again
bool c = foo.equals(foo="some other value"); // Here should be true again, since the compiler takes first the arguments, makes the assignation, and then makes the equals method execution, so in compiler what happens is:
// 1. Make foo = "some other value"
// 2. Compares foo with foo
// Foo is already changed, so is equals to itself

, . - bool c = ... , equals String String.

0

.equals(), a.equals(a) .

equals:

a.equals(a = null);

, :

a.equals(b); or a.equals(null);

You simply compare two different values, typing an expression into equal calls, this does not change.

0
source

A very interesting case is where you have boxing Float, consider this code:

Float alpha = +0.0f;
Float beta = -0.0f;
boolean equal = alpha.equals(beta);
System.out.println("Equal: " + equal);

boolean equality = alpha.floatValue() == beta.floatValue();
System.out.println("Equality: " + equality);

This will print truefor the first and falsefor the second.

The opposite applies to the case Float.NaN.

0
source

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


All Articles