Layers of Logic

Phase VIII

Identity and Contracts

Input, immutability, equality, enums, and interface contracts.

5 sections98 min of reading21 exercises

Trace keyboard and file input through Java's I/O classes, then build immutable value types with explicit equality and hashing rules. Finish with enums and the interface features used by later collection and functional APIs.

When you finish this phase you can

  • Explain the three layers in new BufferedReader(new InputStreamReader(System.in))
  • Explain why buffering reduces calls to an underlying input source
  • Build an immutable class, including defensive copies of mutable data
  • Write equals and hashCode correctly, and state the contract between them
  • Demonstrate how an incorrect equality contract breaks a hash-based lookup
  • Choose an enum over int or String constants and explain the compiler checks gained
  • Define a functional interface, and explain why a lambda needs one
  1. 8.1Java I/O: Streams, Scanner, BufferedReaderRead keyboard and file input with Scanner and BufferedReader, distinguish byte streams from character readers, and explain why buffering improves performance.Stream (I/O)BufferingCore19 min4 exercises
  2. 8.2Immutable ClassesBuild an immutable class by controlling construction, reassignment, mutation, inheritance, and references to mutable data.ImmutabilityCore17 min4 exercises
  3. 8.3The Object Class: `equals`, `hashCode`, `toString`Override equals, hashCode, and toString correctly, and follow the equality contract required by hash-based collections.The Object classequals()hashCode()The equals and hashCode contracttoString()Core24 min5 exercises
  4. 8.4EnumsDefine a fixed set of named values, attach fields and methods to them, and use exhaustive switch expressions safely.enumCore18 min4 exercises
  5. 8.5Interface Methods and Functional InterfacesDistinguish abstract, default, static, private, marker, and functional interface features and choose each for a specific purpose.Default methodFunctional interfaceMarker interfaceCore20 min4 exercises