What does the equals (Object o) mean in this method?

3

In this code:

public boolean equals(Object o) {
        Aluno outro = (Aluno)o;
        return this.nome.equals(outro);

    }

What is it for?

    
asked by anonymous 19.02.2016 / 20:02

1 answer

3

equals(Object o) is, as bfavaretto already said, part of the method signature - here has more details on method signing. This sets the method name ( equals ) and its parameters (a Object called o ).

The equals method defaults to the class Object (all classes in Java inherit from Object ), which is usually to define if an object is equal to another, so it requests an object like parameter.

I could not help noticing that the method code is wrong, it will always return false , because it is comparing an attribute of the current object with the object that was passed by parameter, see:

return this.nome.equals(outro);
//this.nome => objeto atual
//outro => objeto passado por parâmetro

If the intention is to compare the attributes nome , the code should be

return this.nome.equals(outro.nome);
    
19.02.2016 / 20:21