• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Reg == and equals

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the best way to solve code relating to
use of == operator and equals method
( with both primitive dataTypes,
reference dataTypes specifically String related comparisons )
Is someone having a good example covering all possible
comparisons.
Thanks
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Angela, it is not so easy to give a complete list of all possible comparisons. In my understanding, if some class does not override the equals method, it will find and use the equals method in its super class till Object where equals is defined, where the equals() method does the same thing as ==.
And String, File, Date as well as Boolean, Byte, Short, Character, Integer, Long, Float, Double all override the equals() method. The comparison between two objects of them, the result will be true if the two objects are logically the same.
See the following code for more understanding:

Hope it helps,

------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
try my mock exam¹² at my homepage.
 
Guoqiao Sun
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forget to mention that StringBuffer doesn't override the equals method. So the comparison will return false even if the two objects contains the same String inside.
Regards,
------------------
Guoqiao Sun
Sun Certified Programmer for Java™ 2 Platform
try my mock exam¹² at my homepage.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure how much will this be of help to you.... But still
have a look here
 
reply
    Bookmark Topic Watch Topic
  • New Topic