Why is the comparison of objects with the same content false?

Asked

Viewed 66 times

-5

Why this condition is defined as false?

Demonstrando o resultado

I put image to be using the Google console and the code does not need to be executed since the doubt is conceptual.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

2

Objects are types by reference, so the basic values of objects are pointers that indicate where the value of the object really is, is a form of indirect.

When comparing the values of the variables, the comparison is made between pointers. If you have two different objects it is certain that they are at different memory addresses, then obviously the pointers contained in these variables are different, so it is false.

The contents of the objects may be equal, but this is a coincidence, the objects are different, no comparison is made with the contents of the object itself.

Exceptions may exist, for example the type String which is a reference, but has semantics of type by value, so the comparison happens to be made over the content of the object and not its reference, but it was explicitly defined that it would be made like this, it is not the normal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.