Quick revision
The short answer
A record's equals, hashCode and toString are not written out as ordinary bytecode. javac emits a one-line method body containing an invokedynamic instruction whose bootstrap method is java.lang.runtime.ObjectMethods.bootstrap, and the JDK builds the real implementation from the record's component list the first time each call site is linked. At run time, Class.isRecord() and Class.getRecordComponents() describe the components, their types, annotations and accessors, and serialization rebuilds records only through the canonical constructor.
What to remember
In this answer, we saw that a record's equals, hashCode and toString are tiny methods that execute invokedynamic, and that java.lang.runtime.ObjectMethods.bootstrap builds the real implementations from the class, the component names and field getters the first time each call site runs. That keeps class files small and lets the JDK improve the code without recompilation. Primitive components are compared like the wrapper compare methods, so NaN equals NaN and 0.0 differs from -0.0. Class.isRecord and getRecordComponents expose names, types, annotations and accessors, and serialization always goes through the canonical constructor.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A record's equals, hashCode and toString are not written out as ordinary bytecode. javac emits a one-line method body containing an invokedynamic instruction whose bootstrap method is java.lang.runtime.ObjectMethods.bootstrap, and the JDK builds the real implementation from the record's component list the first time each call site is linked. At run time, Class.isRecord() and Class.getRecordComponents() describe the components, their types, annotations and accessors, and serialization rebuilds records only through the canonical constructor.
Why this question is asked
Records look like a pure compiler trick: we write one line and get a class with a constructor, accessors and the three Object methods. Interviewers at this level want to know whether we have looked underneath, because the way those methods are generated explains why class files stay small, why the behaviour can improve with a JDK upgrade, and why frameworks such as Jackson can handle records without guessing. In this answer, we will recap what a record generates, look at the bytecode with javap, follow how invokedynamic links the methods, check how floating point components are compared, and finish with the reflection and serialization support that records get.
1A Quick Recap of What a Record Generates
A record is a class declared with the record keyword. Its header lists its components, the values it carries, and from that header the compiler derives everything else: a private final field per component, a public accessor method named after each component, a canonical constructor, a constructor whose parameters match the components exactly, and implementations of equals, hashCode and toString that use all components.
For the accessors and the constructor the compiler writes ordinary bytecode. The three Object methods are different: their logic is the same for every record, so instead of copying it into every class file, javac delegates it to a shared piece of the JDK.
2Looking at the Bytecode With javap
If we compile Point and run javap -c -v Point.class, the equals method is only a few instructions long. It loads this and the argument, executes invokedynamic, and returns whatever the dynamic call produced. hashCode and toString look the same. The interesting part is the BootstrapMethods table at the bottom of the output.
So the class file carries a description of the record, not an implementation. It says which class this is, what the components are called, and how to read each field. The handles read the fields directly, not through the accessor methods, which is why overriding an accessor does not change what equals compares.
The reason for this design is twofold. Class files stay small, because a record with twenty components does not carry twenty comparisons in its bytecode. And the implementation lives in the JDK, so a later release can make it faster and every existing record benefits without being recompiled.
3How invokedynamic Builds the Method at Link Time
invokedynamic is the instruction Java uses when the target of a call is decided at run time rather than written into the class file. The first time the JVM executes a particular invokedynamic instruction, it calls the bootstrap method named in the table, passing it the static arguments we saw above. The bootstrap method returns a CallSite, an object that holds a method handle to the real code. The JVM links the instruction to that call site, and from then on every execution jumps straight to the linked handle, which the JIT compiler can inline like any normal method.
7 messages in order. Play them, or step through them one at a time.
The bootstrap runs once per call site; later calls go straight to the linked method handle.
Notice that the cost of building the implementation is paid once per call site, not once per call. After linking, a record's equals is as cheap as a hand-written one. Lambdas and string concatenation use the same mechanism.
4How Float and Double Components Are Compared
A detail that surprises people is how primitive components are compared. The generated equals does not use == for them. For each primitive component it behaves like the static compare method of the wrapper class, Integer.compare for int, Double.compare for double and so on, and treats the components as equal when compare returns 0. For integers that is the same as ==, but for floating point it is not.
So a record that holds NaN is equal to itself, which keeps the equals contract intact: equals must be reflexive, and a value that is not equal to itself would be lost in a HashSet. The price is that 0.0 and -0.0 are different records, because Double.compare orders -0.0 before 0.0. Reference components are compared with Objects.equals, so null components are handled without a NullPointerException.
5Reflection Support for Records
Because the compiler also writes a Record attribute into the class file, listing every component, the runtime knows exactly which class is a record and what its state is. Class.isRecord() tells us whether a class is a record, and Class.getRecordComponents() returns one RecordComponent per component, in declaration order. Each RecordComponent gives the name, the type, the generic type, the annotations placed on the component and the accessor Method.
This is what lets Jackson and Bean Validation treat records as data carriers without guessing from method names. Reflection is also stricter for records in one way: the fields are truly final, and Field.set on a record field throws IllegalAccessException even after setAccessible(true).
6How Serialization Treats Records
Java serialization has special rules for records. When a record is written, only its component values are written. When it is read back, the stream values are matched to components by name and passed to the canonical constructor. Ordinary classes are rebuilt by allocating the object without running its constructor and then filling in fields, which is why classic serialization could create objects that skipped all validation.
So any check in a record's compact constructor also runs during deserialization, and a crafted byte stream cannot produce a record that the constructor would refuse. Records also ignore custom readObject and writeObject methods; writeReplace and readResolve are still honoured. Check your version's java.io.Serializable documentation for the full rules.
7Follow-up questions
Can we override the generated equals in a record?
Yes. If we declare equals, hashCode or toString explicitly in the record body, the compiler uses our method and does not emit the invokedynamic version for it. We should then override equals and hashCode together, and keep the result consistent with the record's meaning; overriding equals to ignore a component is legal but usually a sign that the type should not be a record.
Is the hashCode of a record stable across JDK versions?
No guarantee is given. The specification only says that equal records have equal hash codes, and because the implementation is built by the JDK at link time, a later release could change it. Never store record hash codes.
8Conclusion
In this answer, we saw that a record's equals, hashCode and toString are tiny methods that execute invokedynamic, and that java.lang.runtime.ObjectMethods.bootstrap builds the real implementations from the class, the component names and field getters the first time each call site runs. That keeps class files small and lets the JDK improve the code without recompilation. Primitive components are compared like the wrapper compare methods, so NaN equals NaN and 0.0 differs from -0.0. Class.isRecord and getRecordComponents expose names, types, annotations and accessors, and serialization always goes through the canonical constructor.
Open with
For a record, javac does write normal bytecode for the fields, accessors and canonical constructor, but equals, hashCode and toString are each a few instructions that execute invokedynamic.
Then, if they want more
The bootstrap method is ObjectMethods.bootstrap in java.lang.runtime, and the static arguments are the record class, a string with the component names separated by semicolons, and method handles that read each field.
The first time a call site runs, the bootstrap builds a method handle for exactly those components and the JVM links it, so later calls are as fast as hand-written code. The benefit is smaller class files and the fact that the JDK can improve the implementation without us recompiling.
One detail worth knowing is that primitive components are compared like Double.compare or Integer.compare, not with ==, so a record holding NaN is equal to itself, and 0.0 and -0.0 are different.
For reflection, the class file has a Record attribute, so Class.isRecord tells us it is a record and getRecordComponents gives each component's name, generic type, annotations and accessor method. And serialization rebuilds records only through the canonical constructor, so compact constructor validation runs on deserialization too.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How are record equals, hashCode and toString implemented at the bytecode level, and what...": https://scrivio-interview-library.netlify.app/java/records/58f7c1c8ac/