Java Object Contracts

The four contracts every Java object inherits or opts into — equals, hashCode, toString, Comparable — plus why clone is not one of them. The equals properties and how inheritance breaks symmetry, the hashCode obligation and what is stable across JVMs, records' generated implementations and their array and floating-point edges, entity identity under JPA and Hibernate proxies, total ordering and TimSort contract violations, and copying without Cloneable. Use when equals is overridden without hashCode, when an object is a HashMap key, when an entity's equals is built on a database id, when a TreeSet loses an element, when compareTo subtracts, or when a record's generated toString reaches a log. Immutability is java-immutability, null handling inside these methods is java-null-safety, and cross-process hashing is consistent-hashing.

robsonkades ba4a73f 5 files · 41.0 KB Updated

File contents

robsonkades/agent-skills/tree/main/skills/java-object-contracts commit ba4a73f575

Frequently asked questions

npx skillmds@latest add robsonkades/java-object-contracts