O método equals é um objeto da classe Object que compara dois objetos e inclusive os seus Hash codes. Cada vez que criamos um objeto, este objeto recebe um número para identificação q é o seu hash code. Portanto cada instância desta classe possui o seu próprio Hashcode. Portanto use o método equals para saber se os seus objetos são o mesmo objeto.
Aqui está o texto que está no tutorial Java da sun: The equals and hashCode Methods You must override the equals and hashCode methods together. The equals method compares two objects for equality and returns true if they are equal. The equals method provided in the Object class uses the identity function to determine if objects are equal (if the objects compared are the exact same object the method returns true). However, for some classes, two distinct objects of that type might be considered equal if they contain the same information. Consider this code that tests two Integers, one and anotherOne, for equality: Integer one = new Integer(1), anotherOne = new Integer(1); if (one.equals(anotherOne)) System.out.println("objects are equal"); This program displays objects are equal even though one and anotherOne reference two distinct objects. They are considered equal because the objects compared contain the same integer value. Your classes should only override the equals method if the identity function is not appropriate for your class. If you override equals, then override hashCode as well. The value returned by hashCode is an int that maps an object into a bucket in a hash table. An object must always produce the same hash code. However, objects can share hash codes (they aren't necessarily unique). Writing a "correct" hashing function is easy--always return the same hash code for the same object. Writing an "efficient" hashing function, one that provides a sufficient distribution of objects over the buckets, is difficult and is out of the scope of the tutorial. Carlos Santos ----- Original Message ----- From: "Marcio Adriano Batista Leal" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Wednesday, February 26, 2003 10:20 PM Subject: [java-list] Duvida StringBuffer > > Gostaria de Saber porque não apresenta a mensagem OK, pois estou comparando > dois Objetos identicos. > > public class testestring > { > public static void main(String args[]) > { > StringBuffer sb = new StringBuffer("abc"); > StringBuffer s1 = new StringBuffer("abc"); > if (sb.equals(s1)) > { > System.out.println("OK"); > } > } > > > } > > > > > _________________________________________________________________ > MSN Hotmail, o maior webmail do Brasil. http://www.hotmail.com > > > ------------------------------ LISTA SOUJAVA ---------------------------- > http://www.soujava.org.br - Sociedade de Usuários Java da Sucesu-SP > dúvidas mais comuns: http://www.soujava.org.br/faq.htm > regras da lista: http://www.soujava.org.br/regras.htm > historico: http://www.mail-archive.com/java-list%40soujava.org.br > para sair da lista: envie email para [EMAIL PROTECTED] > ------------------------------------------------------------------------- ------------------------------ LISTA SOUJAVA ---------------------------- http://www.soujava.org.br - Sociedade de Usuários Java da Sucesu-SP dúvidas mais comuns: http://www.soujava.org.br/faq.htm regras da lista: http://www.soujava.org.br/regras.htm historico: http://www.mail-archive.com/java-list%40soujava.org.br para sair da lista: envie email para [EMAIL PROTECTED] -------------------------------------------------------------------------