Java Reference
In-Depth Information
Point p = (Point) o;
return p.x == x && p.y == y;
}
@Override
public int hashCode()
{
int hashCode = 19;
int hc = x;
hashCode = hashCode*31+hc;
hc = y;
hashCode = hashCode*31+hc;
return hc;
}
public static void main(String[] args)
{
Point p1 = new Point(10, 20);
Point p2 = new Point(20, 30);
Point p3 = new Point(10, 20);
// Test reflexivity
System.out.println(p1.equals(p1)); // Output: true
// Test symmetry
System.out.println(p1.equals(p2)); // Output: false
System.out.println(p2.equals(p1)); // Output: false
// Test transitivity
System.out.println(p2.equals(p3)); // Output: false
System.out.println(p1.equals(p3)); // Output: true
// Test nullability
System.out.println(p1.equals(null));
//
Output:
false
// Extra test to further prove the instanceof oper-
ator's usefulness.
System.out.println(p1.equals("abc"));
//
Output:
false
Map<Point,
String>
map
=
new
HashMap<Point,
String>();
map.put(p1, "first point");
Search WWH ::




Custom Search