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

Asked

Viewed 367 times

3

In that code:

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

    }

What’s he good for?

  • 3

    equals(Object o) is the signature of the method. equals is a public method that takes an object and returns a boolean value. That’s what you want to know?

  • I don’t know much java, equals wouldn’t that be a private word? @bfavaretto

  • 1

    equals is a class method Object, every java class inherits from Object implicitly.

  • 1

    Nope, equals is a class method Object @rubStackOverflow

  • I don’t get it, I’m reading a booklet from Home of algorithms and data structure.

  • 2

    Is this code right? It seems that you are comparing an object attribute with the other whole object. It wouldn’t be this.nome.equals(outro.nome)?

  • That, I forgot to put the . name on this.nome.equals(other.name)

Show 2 more comments

1 answer

3


equals(Object o) is, like the bfavaretto I have already said, part of the signature of the method - here has more details about method signature. This defines the method name (equals) and its parameters (a Object called o).

The method equals is standard of the class Object (all classes in Java inherit from Object) which usually has the function of defining whether an object is equal to another, so it asks for an object as a parameter.


I couldn’t help but notice 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 attributes nome, the code should be

return this.nome.equals(outro.nome);

Browser other questions tagged

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