What javac does
Records
A final class with fields, accessors and a constructor; toString, equals and hashCode via invokedynamic.
A record is an ordinary final class that extends java.lang.Record. javac writes the private final fields, the accessors and the canonical constructor. toString, equals and hashCode are each one invokedynamic to ObjectMethods.bootstrap, which builds the method at run time from handles to the fields.
The source
// min-release: 16
// A record: fields, accessors and a constructor, plus toString, equals and hashCode via invokedynamic.
public record Point(int x, int y) {
public Point {
if (x < 0 || y < 0) throw new IllegalArgumentException("negative");
}
double length() {
return Math.sqrt(x * x + y * y);
}
}Compiled by javac 25 with -g and --release 17 to 25. Where releases produce different bytecode, each version is shown.
What to look for
- The compact constructor's check runs first; javac adds the field assignments (
putfield) at the end. - All three generated methods share one bootstrap method. Its arguments are the record class, the component names as one string (
"x;y") and a getter handle for each field. - The Record attribute lists the components for reflection and serialization. The JVM itself does not use it.
Point.class
public Point(int x, int y)
double length()
javac --release 17 to 25 (the same bytecode for every release)
public final String toString()
javac --release 17 to 25 (the same bytecode for every release)
public record Point(int x, int y) {public final int hashCode()
javac --release 17 to 25 (the same bytecode for every release)
public record Point(int x, int y) {public final boolean equals(Object o)
javac --release 17 to 25 (the same bytecode for every release)
public record Point(int x, int y) {public int x()
javac --release 17 to 25 (the same bytecode for every release)
public int y()
javac --release 17 to 25 (the same bytecode for every release)
The bootstrap methods
From the BootstrapMethods attribute of the --release 25 class: each invokedynamic names one of these by index.
ObjectMethods.bootstrap(MethodHandles$Lookup, String, TypeDescriptor, Class, String, MethodHandle[]) : Object- Class
Point - String
"x;y" - MethodHandle
getField Point.x : int - MethodHandle
getField Point.y : int
- Class