• 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

Please help

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody please tell me why the following code is giving me
Dog key
null
null
as output
Is there something wrong i have written in the following code...

import java.util.*;

class Dog
{
public String name;

public Dog(String n)
{
name=n;
}

public boolean equals(Object o)
{
if((o instanceof Dog) && (((Dog)o).name==name))
{
return true;
}
else
{
return false;
}
}
public int hashcode()
{
return name.length();
}
}
public class Test
{
public static void main(String args[])
{
Map<Object, Object> mlist=new HashMap<Object,Object>();
Dog d1=new Dog("clover");
Dog d2=new Dog("clover");
mlist.put(d1,"Dog key");
System.out.println(mlist.get(d1));
System.out.println(mlist.get(d2));
System.out.println(mlist.get(new Dog("clover")));

}
}
 
author
Posts: 23956
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
It's the hashCode() method -- with a capital C.

Henry
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shayan please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic