Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Please help  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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: 23811
140
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the hashCode() method -- with a capital C.

Henry
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!