File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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
I agree. Here's the link:
subject: equals
It's not a secret anymore!