I'd like to ask you a question about equlas() in Object class

Asked 2 years ago, Updated 2 years ago, 96 views

I got confused while reading a book.It's two books

One book is

Book 1

Obejct's equlus () returns the same result as the comparative operator ==. True if the same, false if different. False if it's different. Java uses equals() to compare two objects equally. Compare the two objects and return true if they are logically equivalent, otherwise false. Logically equivalent means that the data stored by the object is the same regardless of whether it is the same object or different object.

Object obj1 = new Object();

Object obj2 = new Object();



boolean result = obj1.equlus (obj2); // Same result

Boolean result = (obj1 == obj2) // Same result

Book 2

This method is defined to compare the reference value of the reference variable, just like the == operator.

That's what it says...

If you look at Book 1 with the definition of Book 2, Book 1 is not a comparison of reference values, is it?

obj1 and obj2 are different objects.Reference value (address value) is different. Comparing book 1 with the definition of book 2 Shouldn't it be a different object?

Is the book wrong?

java object

2022-09-20 18:08

3 Answers

By default, == verifies that the two objects point to the same address, and equals() verifies that the two objects have the same value.

However, if you look at the description of book 1, it seems that equals() and == play the same role based on the object Object, and if you think about it, I think book 2 has a similar explanation.

In other words, in the Object object, the method equals() is defined to do the same thing as ==, so each returns the same result.

If you apply it to objects other than Object objects, or if you override the equals() method of a specific object, the results may differ.


2022-09-20 18:08

Obejct's equlus () returns the same result as the comparative operator ==.

It simply seems to be an explanation that the equlus method returns boolean values, such as ==.

Java uses equals() to compare two objects equally. Compare two objects and return true if they are logically equivalent, otherwise false.

It's not your turn to explain the reference variable or reference value yet.

And is the example of book 1 shown in the book or did you add it? The results will both be false.


2022-09-20 18:08

If the Object class "equals" function is not overridden, the return value is equal to "==". If the "equals" of any class is not equal to "==", it is because it has been overridden. In Java, the "equals" method was created with the meaning of comparing the 'values' of the two objects, but the object does not contain data because it is a class that becomes all class parents. Therefore, "equals" act as "==" because there is no value to compare.

In more detail, there is a "general convention" when you override "equals".

To explain one of them,

When there is an object x, x.The return value of equals(x) shall be "true".

There's something like that. For more information, search for "equals General Conventions". Therefore, the general rules of diesel such as the "Object" class were well followed. You could say that.

Of course, Java does not enforce general conventions, nor does it enforce laws. Therefore,

    @Override
    public boolean equals(Object obj) {
        return false;
    }

It doesn't matter if you write it as follows, but this will return false for all situations regardless of the parameter "obj". This is likely to be an unexpected situation when other people code later on. It's not a way to "can't" use it, and there may be situations that need to be used, but it's generally not a desirable way.

So I'm going to answer the exact question, and you're going to get the same false result for the two results up there.

The explanation of book 1 is

"Logically equivalent means that the data stored by the object is the same, regardless of whether it is the same object or different object."

seems to have explained the significance of the "equals" design.

And the explanation of book 2

"Like the == operator, this method is defined to compare reference values of reference variables."

In effect, describes how the Object's equals method is implemented, so both books are correct.


2022-09-20 18:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.