Object methods and contracts
22 questions in 6 subtopics.
Equals and hashCode contract
State the equals contract and thehashCode contract. What breaks in a HashMap if a class overrides equals but not hashCode?Write a correct equals and hashCode for final class Money { private final long cents; private final String currency; }.A HashMap<String[], Integer> never finds keys that look identical. Why?Why is it hard to keep equals symmetric and transitive when a subclass adds a value field, such as ColorPoint extends Point? What are the options?Objects are added to a HashSet, then one of their fields used in hashCode is changed. Later contains returns false and remove does nothing, but iteration shows the object. Explain.Your organization wants a standard for equals and hashCode across domain models, JPA entities, DTOs and records. What would the standard say?Bugs from broken equals and hashCode have appeared in your organization for years: mutable keys, missing hashCode, entity equality mistakes. What would you standardise or forbid, and how do you know it worked?ToString
What doesObject.toString return by default, and what makes a good toString override? What should never be in it?Logging a JPA entity with log.info("{}", order) causes extra SQL queries and once a StackOverflowError. Why?A security audit finds personal data and tokens in logs, mostly coming from toString output of DTOs, records and exceptions. What standards and library support do you introduce across services?Comparable and Comparator
Write a Comparator that sorts employees by department ascending, then salary descending, then name, with nulls in department sorted last.A comparator is written as(a, b) -> a.getId() - b.getId() for int IDs. Sorting occasionally gives wrong order. Why?What does it mean for compareTo to be consistent with equals, and what goes wrong in a TreeSet or TreeMap when it is not? Give a JDK example.You are designing sort and ranking rules for a multi-tenant search API with locale-specific names. What do you need beyond Comparator.comparing(Item::name)?Sort orders are embedded across your systems: in Comparable implementations, database ORDER BY clauses, search indexes and pagination cursors, and they disagree. How do you set a long-term approach to ordering?Clone and copy constructors
Why is Cloneable considered broken, and what do you use instead?Write a deep copy constructor for this class and explain which fields need copying:class Team {
private final String name;
private final List<Member> members;
private final Map<String, Role> roles;
}
Finalize deprecation and Cleaner
Why wasfinalize() deprecated (and deprecated for removal since JDK 18), and what are Cleaner and try-with-resources used for instead?Before a JDK upgrade you want to know whether any of your services or their dependencies still rely on finalization. How do you find out and handle it?Finalization was part of Java from 1.0 and took over twenty years to be deprecated for removal. What does that history say about designing cleanup and lifecycle APIs, and what would you require of your own libraries?Identity vs equality
Explain the difference between == and equals for objects. What doesSystem.identityHashCode return, and when would you use IdentityHashMap?Project Valhalla proposes value classes whose instances have no identity. Explain why Java originally gave every object identity, what that has cost, and how you would prepare a large codebase.No question matches these choices.