This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes equals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals" Watch "equals" New topic


Ramya Iyer
Ranch Hand

Joined: Jul 29, 2003
Posts: 66
Can someone explain this?

The Object class' equals() method just does an = = check, so if (ob1 = = ob2) is false, then ob1.equals(ob2) will always be false
marc weber

Joined: Aug 31, 2004
Posts: 11343

The == comparison operator tests values for equality. When applied to objects, that "value" is basically a reference (e.g., a memory address). Therefore, objA == objB if and only if they are, in fact, the same object (occupying the same space in memory).

The equals method is designed to be overridden to provide a more meaningful comparison of objects. For example, two separate instances of Car might be considered "equal" if they have specific variables in common (e.g., make, model, year, etc.). But in the simple case of Object, the equals method only checks whether the references point to the same object, just as the == operator does.
[ September 07, 2005: 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
wood burning stoves
subject: equals
Similar Threads
Using Objects as Parameters
Using Objects as Parameters
What "type" is the command line args
a doubt in equals() method
Class Already Defined