How does the equals method work?

Asked

Viewed 3,704 times

3

I stopped in an exercise of a book I’m reading and in this shows an example of overloading the method equals, I even understood the concept that he compares the reference between two objects, but in the method: public boolean equals(Object obj) things started to get a little confusing.

My doubts are:

  • What means this return of the method equals: return (getConta() == ((ExemploContaEquals) obj).getConta());
  • Why the if of the main method compares only the last two attributes of the instantiated objects? That is, only compares the numbers 20 and 21 of the objects obj1 and obj2 respectively and the numbers 50 and 50 of the objects obj3 and obj4. What’s the difference then of having two attributes?

    package modulo04.Overload equals;

    public class ExemploContaEquals {
    
        private int conta = 0;
    
        public ExemploContaEquals(int agencia, int conta){
            this.conta = conta;
    
        }
    
        public ExemploContaEquals(){
            this(0,0);
        }
    
    
    
        public int getConta(){
            return conta;
        }
    
    
        public boolean equals(Object obj){
    
            if(obj != null && obj instanceof ExemploContaEquals){
    
                return (getConta() == ((ExemploContaEquals) obj).getConta());
    
            }  else {
                return false;
    
            }
    
        }
    
    }
    

package modulo04.SobrecargaEquals;

public class ExemploContaEqualsPrincipal {

    public static void main(String[] args) {

        ExemploContaEquals obj1 = new ExemploContaEquals(10,20);
        ExemploContaEquals obj2 = new ExemploContaEquals(10,21);

        if(obj1.equals(obj2)){
            System.out.println("Contas iguais");

        } else {
            System.out.println("Conta diferentes");
        }

        ExemploContaEquals obj3 = new ExemploContaEquals(10, 50);
        ExemploContaEquals obj4 = new ExemploContaEquals(20,50);

        if(obj3.equals(obj4)){
            System.out.println("Contas iguais");

        } else {
            System.out.println("Contas difentes");
        }
    }

}

2 answers

7


Well, come on.

On the following point:

What means this return of the equals method: Return (getConta() == ((Examples of match) obj). getConta());

It is checking whether the current instance of the class ExemploContaEquals has (taking the attribute conta as a comparison parameter) an equivalence between the two.

Still on your doubt, the verification is done through a casting, since all classes inherit from Object, allowing comparison to be possible since the object passed in the parameter is an instance of ExemploContaEquals.

And if there is equivalence between the two attributes of the objects used in the comparison, it will be returned true, as a result.

Already about the following doubt:

Why does the if of the main method compare only the last two attributes of instantiated objects? that is, it only compares the numbers 20 and 21 of the obj1 and obj2 objects respectively and the numbers 50 and 50 of the obj3 and obj4 objects.What is the difference then of having two attributes?

In this case, as the method is being overwritten, it is up to the developer to stipulate the parameter that will be taken into account when performing the comparison, properly said.

In this example shown, the author preferred to use the attribute conta as a parameter, but nothing prevents you from deciding to change this later for agency verification, for example, or else include the two attributes as a comparison parameter.

  • So the two parameters are for if I have another attribute? So this(0.0) in the second constructor? now I realize I think that’s what confused me.

  • As I put it, the author wanted to create a very didactic example, trying to elaborate a little and listing some good coding practices. The this(0,0) is to create an object with the agency and nonnull account values, since there is no method set for the attribute conta.

4

The Equals() is a method that comes from the class Object and in its signature awaits an object of the type Object as a parameter.

If the signature of the method were equals(ExemploContaEquals obj), it would not be the same inherited method, inheritance only occurs when the signature is exactly equal.

So it’s made a cast to indicate to the compiler that intends to read this object as if it were an object of type ExemploContaEquals after all you know that is what you want. If you compare an object ExemploContaEquals with a Object, the second shall not have access to ExemploContaEquals even if they are there. The compiler prohibits access to members who are not of the specified type. When you say he’s of that type, then the compiler allows.

Of course, to do this without error you need to make sure that the "conversion" (there is no actual conversion) is possible and for this there is the if previous ensuring that the object is of a compatible type.

The second question I would answer: "because the example tells you to do this," does not seem to have a special reason. Anyway the method has to define what is being compared. It is a "business" decision which will be used, which is relevant to the comparison. I agree that the account alone without the agency is something weird, but it’s just a silly example, it won’t run in a bank :)

Browser other questions tagged

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