Docsity

Java Class Design: Guidelines, Canonical Forms, Equality, HashCode, and Inner Classes, Study notes of Computer Science

Toggle sidebar
bg1
1/14/2009
1
Class Design
CS 3331
Question
public class StudentBody1 {
private ArrayList students;
/* … */
public void addAll(ArrayList students) { /* … */ }
}
public class StudentBody2 {
private List students;
/* … */
public void addAll(List students) { /* … */ }
}
Design Guidelines
•G1: Avoid public fields
•G2: Make public interfaces complete
•G3: Separate interface from implementation
Canonical Form of Classes
// E.g., what will be printed?
List points = new LinkedList();
points.add(new Point(10, 20));
If (points.contains(new Point(10, 20))) {
System.out.println(“Found!”);
} else {
System.out.println(“Not found!”);
}
Canonical Form (Cont.)
•Canonical forms of public classes
–String representation
–No-argument constructor
–Object equality
–Cloning
–Serialization
Equality
•Identity equality vs. value quality
“==“ vs. equals()
“==“ implies equals()
Is “p1 == p2” ?
Is “p1.equals(p2)” ?
Is “p1 == p3” ?
Is “p1.equals(p3)” ?
: Point
x = 10
y = 20
p1:
: Point
x = 10
y = 20
p2:
p3:
Point p1 = new Point(10, 20);
Point p2 = new Point(10, 20);
Point p3 = p1;
pf3

Partial preview of the text

Download Java Class Design: Guidelines, Canonical Forms, Equality, HashCode, and Inner Classes and more Study notes Computer Science in PDF only on Docsity!

Class Design

CS 3331

Question

public class StudentBody1 { private ArrayList students; /* … / public void addAll(ArrayList students) { / … / } } public class StudentBody2 { private List students; / … / public void addAll(List students) { / … */ } }

Design Guidelines

  • G1: Avoid public fields
  • G2: Make public interfaces complete
  • G3: Separate interface from implementation

Canonical Form of Classes

// E.g., what will be printed? List points = new LinkedList(); points.add(new Point(10, 20)); If (points.contains(new Point(10, 20))) { System.out.println(“Found!”); } else { System.out.println(“Not found!”); }

Canonical Form (Cont.)

  • Canonical forms of public classes
    • String representation
    • No-argument constructor
    • Object equality
    • Cloning
    • Serialization

Equality

  • Identity equality vs. value quality “==“ vs. equals() “==“ implies equals() Is “p1 == p2”? Is “p1.equals(p2)”? Is “p1 == p3”? Is “p1.equals(p3)”? : Point x = 10 y = 20 p1: : Point x = 10 y = 20 p2: p3: Point p1 = new Point(10, 20); Point p2 = new Point(10, 20); Point p3 = p1;

Equality (Cont.)

  • Default implementation of equals method
    • Inherited from the class Object
    • Test for object identity
  • Can be overridden by subclasses Contract of Equality
  • Equivalence relation
  • Reflective: x.equals(x)
  • Symmetric: x.equals(y) y.equals(x)
  • Transitive: x.equals(y) y.equals(z) x.equals(z)
  • Consistency
  • x.equals(y) consistently returns true or false
  • Nonnullity
  • x.equals(null) always returns false The hashCode Method
  • Purpose
  • Returns the hash code of an object
  • Used by hash-table-based collection classes (e.g., HashMap, HashSet)
  • Contract for hashCode
  • x.equals(y) x.hashCode() == y.hashCode() Defining hashCode Methods
  • General scheme
  • Compute hash code for each significant field
  • Combine hash code of all significant fields public int hashCode() { int result = 0; // accumulative hash code int h; // hash code for a field << for each field compute and combine the hash code >> return result; } Computing hash code for fields
  • boolean fields
  • f? 0: 1
  • byte, char, short, and int fields
  • (int) f
  • long fields
  • (int)(f ^ (f >>> 32))
  • float fields
  • Float.floatToIntBits(f)
  • double fields
  • Double.doubleToLongBits(f) and then to int
  • References
  • If null, then 0 (or some fixed value),
  • Recursive equals recursive hash on fields, or
  • Hash on canonical representation How to Combine Hash Code?
  • Bitwise-or (|) result = result << n | h; where n is an arbitrary integer constant, e.g., 8.
  • Addition result = result * p + h; where p is a prime number, e.g., 37.