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
Author

.equals( )

Sadaf Zaidi
Greenhorn

Joined: Oct 09, 2001
Posts: 29
Can anybody tell me .equal method. How it works.
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
The equals() method is actually defined in the Object class. This is straight from the 1.3 API:

public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation:
It is reflexive: for any reference value x, x.equals(x) should return true.
It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified.
For any non-null reference value x, x.equals(null) should return false.
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
Parameters:
obj - the reference object with which to compare.
Returns:
true if this object is the same as the obj argument; false otherwise.

Basically the equals(Object obj) method checks to see if the references refer to the "same" object. Some classes, like String for example, override this method so it checks if the actual objects (2 separate objects) referenced by the two arguments are "equal", or have the same value.
If you design a class that explicitly or implicitly extends class Object, you inherit this method as well. But if you want it to check the actual objects for equality, you need to override it also to do so.
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud


Michael J Bruesch<br /><i>I code, therefore I am.</i>
Argm Mastoi
Ranch Hand

Joined: Oct 16, 2001
Posts: 35
hi sadaf,
onestring.equals(secondstring)
compares the onestring with the secondstring according to dictionary style and this is a deep comparision and if onestring is equals to secondstring then it gives u true otherwise it returns false
Sadaf Zaidi
Greenhorn

Joined: Oct 09, 2001
Posts: 29
Originally posted by Argm Mastoi:
hi sadaf,
onestring.equals(secondstring)
compares the onestring with the secondstring according to dictionary style and this is a deep comparision and if onestring is equals to secondstring then it gives u true otherwise it returns false


Thanx alot Argm
Sadaf Zaidi
Greenhorn

Joined: Oct 09, 2001
Posts: 29
Originally posted by Sadaf Zaidi:
Originally posted by Argm Mastoi:
[b]hi sadaf,
onestring.equals(secondstring)
compares the onestring with the secondstring according to dictionary style and this is a deep comparision and if onestring is equals to secondstring then it gives u true otherwise it returns false


Thanx alot Argm
[/B]

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: .equals( )
 
Similar Threads
Think
Hibernate supports equals and not equals
WA #1.....word association
juice
a question waiting to be cleared