As for the first question, you are correct.
Second:
Not necessarily. In Java the method Equals()
always looking for (or at least should, depends on the actual implementation) find the most descriptive comparison. You can even in some cases compare the reference, when this is the most descriptive way of comparing objects. But in principle the reference comparison is avoided, when possible.
The best example is the comparison of strings. Two strings identical but at different addresses return true
when using Equals
but false
when using ==
(That’s a little more complicated than this because of interning, but this is another matter).
So for your specific case, it would be the same thing. Unless the implementation of Equals
class specific Pessoa
change the default behavior.
By default Java, through the class Object
where all other classes are derived directly or indirectly, has an implementation of the Equals
comparing the references.
But it would be fitting that this class Pessoa
superimposed the standard implementation with another that was more descriptive. To take some (s) key(s) field(s) and compare them to determine whether it is the same identity or not. These fields would need to form a single key, i.e., no other instance of the class could be equal to another.
Is there this implementation? Only you can see it, we are not seeing it. You have to see in this class whether there is this implementation and how it is proceeding.
In the comment from Anthony Acciolly above has a link to his blog with a good example of implementing using CPF as the person’s unique identity identifier. It’s a good way to make the Equals
class Pessoa
more relevant. Note that there is nothing extraordinarily wrong with comparing references in this case. But it is not the most intuitive. The ideal is to make this comparison have a semantics of its own. See how it did the implementation of Equals
for the class thinking about the various situations of the object:
@Override
public boolean equals(Object obj) {
// Um objeto é sempre igual a ele mesmo
if (this == obj) {
return true;
}
// Um objeto nunca deve ser igual a null
if (obj == null) {
return false;
}
/* Uma pessoa só pode ser igual a outra pessoa.
* Caso uma pessoa possa ser igual a uma de suas subclasses
* use !(obj instanceof Pessoa)
*/
if (getClass() != obj.getClass()) {
return false;
}
// Converte a referencia para uma pessoa
final Pessoa other = (Pessoa) obj;
// Duas pessoas só podem ser comparadas se possuem CPF
if (this.cpf == null || other.cpf == null) {
return false;
}
// Duas pessoas são iguais se possuem o mesmo CPF
return this.cpf.equals(other.cpf);
}
I put in the Github for future reference.
See the implementation details on his blog (he kindly provided this example).
I talk about it in more detail in that reply. It is about C# and has some differences for Java, but there I explain better this identity thing. I also speak a little identity here in that reply.
Sorry about the "ad" but I wrote a post about it on the blog. It is a bit extensive to be ported in full here: http://a.accioly.7rtc.com/2011/05/java-vs-equals.html
– Anthony Accioly