• 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

SCJP1.4 : Confusion in equals() method

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I'm preparing for SCJP1.4 certification.

I learned that I need to override the System.Object class's 'boolean equals(Object o)' method in my Class if I want to use my Class object(say myObj) as the key in a Hashtable.If i do not do the same, then I can not search the HashTable unless I have the same reference:

I wrote the following code, but I am getting the NullPointerException on the last statement:

import java.util.Hashtable;

public class Equals {
private int id;
Equals(int id){this.id=id;}

public boolean equals(Object obj)
{
if((obj instanceof Equals)&&(this.id==((Equals)obj).id))
{return true;}
else
{return false;}
}
public static void main(String[] args) {
Hashtable tbl=new Hashtable();

Equals eq1=new Equals(1);

tbl.put(new Equals(1),"1");
System.out.println(tbl.get(eq1).toString());
}
}

Please tell me if I understood the concept wrong.

Thanks and Regards.
Kuldeep.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you learn about overriding the hashCode method too ?
You'll need to return the same hashCode to make to objects equal.
 
Kuldeep Tewari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! Satou

I haven't gone thru the hashCode() topic yet.I came across equals() topic first in Bates and Sierra's certification book.

Gonna take the hashCode() topic now.

Regards.
Kuldeep.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there .. according to the offical java contract whenever u override equals u need to override hashcode as well. But in case u dont do it, the hashtable of the Object class .. the granddaddy of all the classes..would be inherited.. n it would be used..

the problem with ur code are as follows:
1. ur not using "tbl.put(new Equals(1),"1");" properly ur using a new object of Equlas as a key ....which needs hashcode (n u dont have it.. rt?)

2 "tbl.get(eq1).toString());" here ur trying to get the stored value using the older object created in "Equals eq1=new Equals(1);"..


So since both are different objects (though equal. but without hashcode).. u wont get ur stored value in the used HashTable.

Try this may be u get somthin here string is used as a key which has hashcode overiden

import java.util.Hashtable;

public class Equals {

private int id;

Equals(int id){
this.id=id;
}

public boolean equals(Object obj){
if((obj instanceof Equals)&&(this.id==((Equals)obj).id))return true;
else return false;
}

public static void main(String[] args) {
Hashtable tbl=new Hashtable();
Equals eq1=new Equals(1);
tbl.put("1",eq1);
System.out.println(tbl.get("1").toString());
}


}
 
Kuldeep Tewari
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Akash!
 
reply
    Bookmark Topic Watch Topic
  • New Topic