I am preparing for an interview, and then made for myself a couple of discoveries:

1) Do I understand correctly that equals () for a custom class will always return false by default until you override it?

2) And, it turns out, when you write something like this

public class HelloWorld{ public static class Lol { private int a; Lol(int a) { this.a = a; } public boolean equals(Lol other){ return this.a == other.a; } } public static void main(String []args){ Lol lol1 = new Lol(1); Lol lol2 = new Lol(2); System.out.println(lol1.equals(lol2)); } } 

inside the equals () method, you can even access the private fields of the argument. Why is this so?

  • Inside one class you can refer to the closed fields of the same class. And you try to scatter them in different files. Will it succeed then? - Garden Chamber

3 answers 3

1) Do I understand correctly that equals () for a custom class will always return false by default until you override it?

The equals method is inherited from the Object class and works as follows:

The most discriminating possible equivalence relation on objects; If this is the case, it is true that you can use it for any non-null reference values ​​(x == y has the value true).

That is, it returns true if x and y refer to the same object. Therefore, until equals is redefined, this behavior will be preserved, and therefore, even though all fields are duplicated in duplicate, the result of equals will be false if the compared objects are not the same object in memory (otherwise the result will be -that's true).

An important remark: if you submit this code at the interview, you are very likely to receive a question in response if you also forgot to override the hashCode method. The correct answer is forgotten, these two methods always go together. In particular, in C # (in java I don’t know, maybe, too) when overriding only Equals, you will receive a warning from the compiler that it would be nice to override GetHashCode.

2) And, it turns out, when you write something like this (...) inside the equals () method you can access even the private fields of the argument. Why is this so?

Of course, a class within its own can refer to its own private fields (otherwise what would be the meaning of their existence). Here it should be understood that privacy is provided in relation to the class "in general", and not to specific instances. That is, from any instance of the class Foo, you can get access to the private items of any other instance of the same class, but you cannot access this from instances of other classes.

UPD:
In fact, this has a very large practical meaning - there are a lot of situations where an instance of a class must interact with other instances of the same class and have access to their members. An example is the same equals (it is quite logical that for a correct comparison of two instances it is necessary to compare their internal data), a copy constructor, or various operations related to the comparison. If this were not possible, to implement such methods, it would be necessary to bring some of the internal logic out, making private fields public or giving the external code access to them through getters / setters, which is often destructive or simply unnecessary.

  • And thank you for the last paragraph, for some reason I thought that privacy is provided in relation to a specific instance. And thanks for the remark about hashCode. - sinedsem
  • @Dazar, read, please, the last paragraph again, there I added a rather important addition to what was necessary exactly. - DreamChild
  • @DreamChild, thanks. - sinedsem

equals () for a custom class will always return false by default until you override it?

No not always. The implementation of equals in java.lang.Object as follows:

 public boolean equals(Object obj) { return (this == obj); } 

Thus, the default equals implementation returns true if both objects refer to the same memory area, in other words, if the object is compared with itself.

inside the equals () method, you can even access the private fields of the argument. Why is this so?

And what surprises you is that inside the class method you can access the private fields of the same class?

PS By the way, in your example you did not redefine equals . Simply put, they created not the overrided method , but the overloaded method . And since overriden methods are selected in runtime, for example, your equals will not be called with collections (due to type erasure ). Change the type of the argument to Object .

  • And, yes, thanks for the PS, I usually do it through the IDE, but now I just typed in memory). - sinedsem

1.equals () for a user class defaults to references, so you need to write it manually or using IDE tools.