• 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

public boolean equals (Object obj)

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
I have an interesting problem with the following code. Whenever I try to run this program it gives me false(No!) for (date.equals(date2)) method even though it should return true(Yes!). I tried to figure out the fault using debugger but could not succeded. Can anyone please help me?

Mahi.


EDIT by mw: Added Code Tags.
[ February 16, 2007: Message edited by: marc weber ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Your date is a reference of type Date. But your date1, date2, and date3 are all of type Object.

You've overloaded the equals method to take a Date object, but when you call it using an Object as an argument, the equals method inherited from Object is invoked. (Remember, calls to overloaded methods are resolved based on the arguments' reference types -- not their true runtime types. This is different than overridden method calls, which are resolved based on the calling object's true runtime type.)
[ February 16, 2007: Message edited by: marc weber ]
 
Mahi Rahman
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marc!
I now understand what was the flaw in my code. Also thanks for editing with [CODE] tags. Cheers!
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic