File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes object equals() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "object equals() method" Watch "object equals() method" New topic
Author

object equals() method

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477

since GregorianCalendar doesnot override equals() method.so objects equals() method will use.so == comparion shallow comparison which will just look for both references point to the same object.i thought three would give false.

But how come the answer is true false true.Please help.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9472
    
    2

Hi Above,

The equals method compares two things. One if they are from the same class and two if the contents of both the objects are the same. Anyone correct me if I'm wrong.

Regards,
Jothi Shankar Kumar. S


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
mmmmmm.Jothi thats for overrided equals() method.but here,Calender class doesnot override equals() method.Am i thinking right or wrong?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9472
    
    2

Hi Shiva,

Consider the code below,



What do you think would that print? As you say I'm not overriding the equals method. Given the above case, it takes the equals method from the Object class and prints "Both are not equal". Reason, we are applying equals on two different types (i.e, String and StringBuffer types).

Now consider the below code,



What would the above code print. Here still I'm not overriding the equals method and it will use the one in the Object class. So above code prints Both are equal....Can you spot the difference between the two?? The second case returns true because the equals method makes sure that both the objects are belonging to the same type. This is a similiar situation to your GregorianCalender case where the comparison(equals) is made for the same type i.e. GregorianCalender obj.equals(GregorianCalender obj) which always returns true. Hope you got the idea.

Anyone any comment on this?

Regards,
Jothi Shankar Kumar. S

[ November 01, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
[ November 01, 2006: Message edited by: Jothi Shankar Kumar Sankararaj ]
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Shiva,The Calender class has a overrided equals().The newly created GregorianCalendar object will pass equals() test but will fail the == test,as == comprise of reference equality check.


~Sanjeev Singh<br />SCJP 1.5
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Thanks for the help jothi and sanjib.I got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: object equals() method