Equality, hashing and mutable keys
Ordering, scale and normalization
Unicode and testable time
Reference subtopics
Object methods and hash keys
getClass reports runtime type. toString should help diagnosis without leaking secrets. equals must be reflexive, symmetric, transitive, consistent and false for null. Equal values must share hashCode. A mutable hash key can become unfindable after insertion. Identity-based lookup is special-purpose and uses reference identity by design.
Comparable and Comparator
Comparable gives natural ordering. Comparator.comparing and thenComparing make sort keys explicit. If a comparator returns zero for unequal objects, a sorted set treats them as one ordering key. Compare numeric values with Integer.compare or comparingInt to avoid subtraction overflow.
Strings and joining
String is immutable. Literals and constant string expressions may be interned; intern returns a canonical pooled string. Use equals for text. Prefer StringBuilder for repeated single-threaded appends, StringJoiner for delimiter-aware output, and understand that synchronized StringBuffer methods do not automatically make a multi-call sequence atomic.
Unicode and encoding
A char stores one UTF-16 code unit. An emoji can require a surrogate pair, so String.length need not count code points or visible graphemes. Use codePoints for code-point logic and StandardCharsets.UTF_8 for explicit bytes. Do not truncate user-visible text by blindly cutting at a char boundary.
Money and time
BigInteger handles arbitrary-precision integers; BigDecimal handles decimal scale and rounding. equals includes scale, compareTo compares numeric value. For time, Instant identifies a timeline point, LocalDate a calendar date and ZonedDateTime zone-aware local time. Period is calendar-based; Duration is elapsed time. Test DST gaps and overlaps and inject Clock for reproducible expiry rules.
Run the complete Java labs ↗ · Java reference ↗