Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

.equals( )

 
Sadaf Zaidi
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody tell me .equal method. How it works.
 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Argm Mastoi
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic