Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java code related to equals method

Tags:

java

equals

I am practicing for an exam, and found a sample problem that I don't understand.

For the following code, find what the output is:

public class Test {      private static int count = 0;      public boolean equals(Test testje) {         System.out.println("count = " + count);         return false;     }      public static void main(String [] args) {         Object t1 = new Test();         Object t2 = new Test();         Test t3 = new Test();         Object o1 = new Object();          ++count; t1.equals(t2);         ++count; t1.equals(t3);         ++count; t3.equals(o1);         ++count; t3.equals(t3);         ++count; t3.equals(t2);     } } 

The output of this code is count = 4, but I don't understand why. Can anyone help me?

like image 495
Dries Coppens Avatar asked Jan 15 '16 14:01

Dries Coppens


People also ask

What is equals () method in Java?

Java String equals() Method The equals() method compares two strings, and returns true if the strings are equal, and false if not. Tip: Use the compareTo() method to compare two strings lexicographically.

What is equals () and hash code () method?

Equals() and Hashcode() in Java. The equals() and hashcode() are the two important methods provided by the Object class for comparing objects. Since the Object class is the parent class for all Java objects, hence all objects inherit the default implementation of these two methods.

How hashCode () and equals () method are used in HashMap?

In HashMap, hashCode() is used to calculate the bucket and therefore calculate the index. equals() method: This method is used to check whether 2 objects are equal or not. This method is provided by the Object class. You can override this in your class to provide your implementation.


2 Answers

The first thing you should note is that public boolean equals(Test testje) doesn't override Object's equals, since the argument is Test instead of Object, so the signatures don't match.

Therefore the main method calls equals(Test testje) exactly once - when executing t3.equals(t3); - since that's the only case in which both the static type of the instance equals is executed for and the type of the argument are the Test class.

t3.equals(t3); is the 4th equals statement (which comes after 4 increments of the static count variable), so 4 is printed.

All the other equals statements execute Object's equals, and therefore print nothing.

A more detailed explanation :

t1.equals() calls Object's equals regardless of the type of the argument, since the static (compile time) type of t1 is Object, and the Test class doesn't override that method. The Object class doesn't have an equals method with a single Test argument, so equals(Test testje) can't be called, regardless of the dynamic (runtime type) of t1.

t3.equals() can execute either Object's equals or Test's equals, since the compile time type of t3 is Test, and the Test class has two equals methods (one inherited from the Object class and the other defined in the Test class).

The method being chosen depends on the compile time type of the argument : 1. When the argument is Object (as in t3.equals(o1); or t3.equals(t2);), Object's equals is called and nothing is printed. 2. When the argument is Test, as in t3.equals(t3);, both versions of equals match that argument, but due to the rules of method overloading, the method with the most specific argument - equals(Test testje) - is chosen and the count variable is printed.

like image 75
Eran Avatar answered Sep 16 '22 20:09

Eran


The equals method in Test takes an instance of Test.

All the previous attempts have been made with an instance of Object, which take the inherrited method from the Object class:

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

Since there is no print in there, it won't print any value.

Your ++count; will increment the value of count, so the moment when you actually call your

public boolean equals(Test testje){... 

method, that does print that value, the value of count is 4.

like image 22
Stultuske Avatar answered Sep 16 '22 20:09

Stultuske