• 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

override .equals() and .hashcode()

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

This question comes from Sierra / Bates chapter 7, question 4.

Which class does not override the equals() and hashcode() methods, inheriting them directly from class object?

A. java.lang.String
B. java.lang.Double
C. java.lang.StringBuffer
D. java.lang.Character
E. java.util.ArrayList


The correct answer is c. I want to understand the logic of this question so that, if necessary, I can answer a similar question from a different list of classes??
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All Wrapper classes, the String Class and the Collections classes override the equals() and the hashcode() methods.

I dont know wht you mean by logic...do u mean why the classes that override, override them?
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The #1 thing to remember here - because it can freak the unwary - is that the StringBuffer class does not implement equals() and hashCode(). Probably because it was conceived simply for string manipulation rather than representation (we've got the String class for that). Classes that represent or contain data generally have hashCode() and equals() implementations.

- Peter
reply
    Bookmark Topic Watch Topic
  • New Topic