HexplainedJavaThread dumpsHeap dumpsAbout
ViewerFormatWhat javac doesOpcodesConstantsAttributesVersionsJAR
Java class files › What javac does › Records

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)

javac --release 17 to 25 (the same bytecode for every release)

4public Point {
0aload_0 // this
1invokespecial #1// new Record()
5if (x < 0 || y < 0) throw new IllegalArgumentException("negative");
4iload_1 // x
5iflt 12
8iload_2 // y
9ifge 22
12new #7// IllegalArgumentException
15dup
16ldc #9// "negative"
18invokespecial #11// new IllegalArgumentException(String)
21athrow
4public Point {
22aload_0 // this
23iload_1 // x
24putfield #14// Point.x : int
27aload_0 // this
28iload_2 // y
29putfield #20// Point.y : int
6}
32return

Open in the viewer

double length()

javac --release 17 to 25 (the same bytecode for every release)

9return Math.sqrt(x * x + y * y);
0aload_0 // this
1getfield #14// Point.x : int
4aload_0 // this
5getfield #14// Point.x : int
8imul
9aload_0 // this
10getfield #20// Point.y : int
13aload_0 // this
14getfield #20// Point.y : int
17imul
18iadd
19i2d
20invokestatic #23// Math.sqrt(double) : double
23dreturn

Open in the viewer

public final String toString()

javac --release 17 to 25 (the same bytecode for every release)

3public record Point(int x, int y) {
0aload_0 // this
1invokedynamic #29// toString(Point) : String ⇐ ObjectMethods.bootstrap "x;y"
6areturn

Open in the viewer

public final int hashCode()

javac --release 17 to 25 (the same bytecode for every release)

3public record Point(int x, int y) {
0aload_0 // this
1invokedynamic #33// hashCode(Point) : int ⇐ ObjectMethods.bootstrap "x;y"
6ireturn

Open in the viewer

public final boolean equals(Object o)

javac --release 17 to 25 (the same bytecode for every release)

3public record Point(int x, int y) {
0aload_0 // this
1aload_1 // o
2invokedynamic #37// equals(Point, Object) : boolean ⇐ ObjectMethods.bootstrap "x;y"
7ireturn

Open in the viewer

public int x()

javac --release 17 to 25 (the same bytecode for every release)

3public record Point(int x, int y) {
0aload_0 // this
1getfield #14// Point.x : int
4ireturn

Open in the viewer

public int y()

javac --release 17 to 25 (the same bytecode for every release)

3public record Point(int x, int y) {
0aload_0 // this
1getfield #20// Point.y : int
4ireturn

Open in the viewer

The bootstrap methods

From the BootstrapMethods attribute of the --release 25 class: each invokedynamic names one of these by index.

  1. 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
← Inner classes and nestmatesAll topicsPattern-matching switch →

By the author of this site

Reading bytecode is half the story; running it is the other half. You execute exactly this kind of code in Build a JVM in Rust: Class files, an interpreter, objects and invokedynamic from scratch, on the real JDK class library.

Read Chapter 1 free

Java and the JVM are trademarks of Oracle; this site is not affiliated with Oracle. Field names follow the Java Virtual Machine Specification; the explanations are our own.

Sister sites: What's in my DB · LLM Anatomy · Distribution Atlas · Notation Decoder · uresk.net

Made by Spencer Uresk · About · Files you open stay on your device.