http://www.math.grin.edu/~rebelsky/Courses/397/98S/students.html
http://www.math.grin.edu/~rebelsky/Glimmer/programmer.html
== compares two references to see that they refer
to the same object
equals() compares two references to see if the
objects they refer to are somehow equivalent. It is your
responsibility to write this method for your own classes.
equals()? Well, it depends
on our class, since different classes will have different notions
of equality.
Fraction class, we'd like the numerator and
denominator to be the same.
Position class, we'd like all the components to
be the same.
equals() should be.
equals()
Objects).
instanceof operator.
/** A two-dimensional point in a discrete space. */
public class Point2D {
/** The x coordinate */
private int x;
/** The y coordinate */
private int y;
...
/**
Compare another point to the current point.
pre: (none)
post: true is returned if they are the same; false o/w
*/
public boolean equals(Point2D compare_to) {
return (x == compare_to.x) && (y == compare_to.y);
} // equals
/**
Compare another object to the current point.
pre: (none)
post: true is returned if the other object is a point and
is the same point; false o/w
*/
public boolean equals(Object compare_to) {
if (compare_to instance_of Point2D) {
return equals((Point2D) compare_to);
}
else return false;
} // equals
} // Point2D
lessThan() method may suffice.
Comparable class, and requiring that all
classes that supply lessThan() extend Comparable.
But what if we want to extend another class, too?
Comparable interface.
Comparable
interface? Arguably, no ...
equals() is supported by all objects.
lessEqual() can be written as a hybrid of
lessThan() and equals()
greatherThan can be written as !lessEqual()
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Mon Nov 10 07:55:26 1997.
This page generated on Mon Nov 10 08:50:55 1997 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu