• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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
Sheriff
Posts: 23571
138
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: 9613
37
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...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!