Loading topic
java.lang.Object. This chapter shows where those methods come from and what they do by default. It also explains why the defau5 min readString, List, and Map. By the end you should be able to predict what == and equals return for strings, boxed numbers, enums, and your own classes, and you shou5 min readequals method must follow, why each rule exists, and how real code breaks them. Writing a correct equals step by step co5 min readequals compares objects, and you have used HashMap and HashSet. Here you learn the rules hashCode must follow, the code that breaks them, and how to tell a legal implementation from a good one.5 min readequals and hashCode are defined and what the two contracts say. It also assumes you have used Set and Map from java.util. Here you will see how HashMap and HashSet use the two methods together, and why a class that overrides only one of them behaves wr6 min readHashMap and HashSet use hash codes. Here you write hashCode methods that stay consistent with equals, choose the right helper for each field type, an5 min readObject is the root of every class, and have used List and Map from java.util. Here you will see what toString is meant for, what Object prints when you don't override it, and where the JVM calls it for you.5 min readtoString output ClassName@1b6d3586. It also assumes you know how equals and hashCode are overridden. Here you will decide what a good toString should print and avoid the bugs th4 min readequals and hashCode pair for a class with int and String fields. It covers three kinds of field where the obvious code fails: double and float values, arrays, and references that may be null. Each has its own rule for comparing and has5 min readHashMap and HashSet use hashCode and equals, and that you can write both methods for a value class. It covers one failure that follows from those rules. If an object's hash code changes while it sits in a hash-based collection, the collection can no longer f6 min readList, Set, and Map. The chapter covers a single mistake: declaring an equals method that looks right but does not replace Object.equals. 5 min readequals and hashCode for a final value class, and that you know the equals contract (reflexive, symmetric, transitive, consistent, false for null). It covers what happens when a subclass enters the picture, and which of three strategies to choose.5 min readequals and hashCode contracts, how HashMap and HashSet use them, and the inheritance pitfalls. It also assumes you know Java 16 or later syntax at the level of instanceof pattern matching. Records are the language's answer to t5 min readequals and hashCode contracts are easy to break without noticing. A broken class compiles, passes a quick manual check, and then misbehaves later inside a HashMap. This chapter shows how to write tests that catch those breaks.4 min read