• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals and hashcode

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Do you know why the following program cannot get the result -12 1? JVM seems think object n1 and n2 are different. I have alrady override the equals() and hashcode() methods but I still cannot make the JVM think the object n1 and n2 are the same.

Hope someone can help me.

import java.util.*;
class Nearly {
String value;
Nearly(String v) {
value = v;
}

public int hashcode() {
return 1;
}
public boolean equals(Object o) {
if ((o instanceof Nearly)) {
if (value.charAt(0) == ((Nearly) o).value.charAt(0)) return true;
}
return false;
}
public static void main(String[] sss){
Nearly n1 = new Nearly("aaa");
Nearly n2 = new Nearly("aaa");
String s ="-";
if (n1.equals(n2)) s += "1";
if (n1 == n2) s+= "2";
Set <Nearly> set = new HashSet<Nearly>();
set.add(n1);
set.add(n2);
System.out.println(s + " " + set.size());
}
}
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to override the hashCode() method -- notice the capital C.

Henry
 
kenneth See
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Henry,

Thanks a lot. It works. I just have one more question in my program:

n1 and n2 have the same hashcode and same value ("aaa"). Why n1==n1 is still not true? It is because I still cannot get it print out the result -12 1.

Thanks for your help.

Kenneth
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

n1 and n2 have the same hashcode and same value ("aaa"). Why n1==n1 is still not true?



Because they are not the same object. The equals() method is used to determine if two objects are equal in value. The "==" operator is used to determine if both references are referring to the same object.

In this example, it is two different objects, that have the same value. They are considered to be equal, but they are not the same object.

Now, if you had done this...



Then they would be the same object.

Henry
[ November 06, 2008: Message edited by: Henry Wong ]
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic