File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes public boolean equals (Object obj) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public boolean equals (Object obj) " Watch "public boolean equals (Object obj) " New topic
Author

public boolean equals (Object obj)

Mahi Rahman
Greenhorn

Joined: Feb 16, 2007
Posts: 21
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 ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Mahi Rahman
Greenhorn

Joined: Feb 16, 2007
Posts: 21
Thanks Marc!
I now understand what was the flaw in my code. Also thanks for editing with [CODE] tags. Cheers!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: public boolean equals (Object obj)
 
Similar Threads
Current Date Comparison
Problem with saving a state in calendar program.
How to Compare two Calendar Objects.
Comparing two date inputs
"date" sorting program