• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object equals() method

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 10110
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10110
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help jothi and sanjib.I got it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic