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

What javac does

assert

A static flag set from desiredAssertionStatus(), checked before each assertion.

assert compiles to a test of a synthetic static field, $assertionsDisabled, which the static initialiser sets from Class.desiredAssertionStatus(). With assertions off, the default, each assert costs a getstatic and an ifne that skips it, and the JIT compiler removes even that because the field is final.

The source

// assert: a static flag set from desiredAssertionStatus(), and an AssertionError.
public class Asserts {
    static int half(int n) {
        assert n % 2 == 0 : "odd: " + n;
        return n / 2;
    }
}

Compiled by javac 25 with -g and --release 8 to 25. Where releases produce different bytecode, each version is shown.

What to look for

  • The message is built only when the assertion fails, after the test, so an expensive message costs nothing while it holds.
  • A failed assertion is an ordinary new AssertionError and athrow.
  • With --release 8 the message is built with a StringBuilder; from 11 on, with invokedynamic.

Asserts.class

static int half(int n)

javac --release 8

4assert n % 2 == 0 : "odd: " + n;
0getstatic #7// Asserts.$assertionsDisabled : boolean
3ifne 39
6iload_0 // n
7iconst_2
8irem
9ifeq 39
12new #13// AssertionError
15dup
16new #15// StringBuilder
19dup
20invokespecial #17// new StringBuilder()
23ldc #18// "odd: "
25invokevirtual #20// StringBuilder.append(String) : StringBuilder
28iload_0 // n
29invokevirtual #24// StringBuilder.append(int) : StringBuilder
32invokevirtual #27// StringBuilder.toString() : String
35invokespecial #31// new AssertionError(Object)
38athrow
5return n / 2;
39iload_0 // n
40iconst_2
41idiv
42ireturn

Open in the viewer

javac --release 11 to 25

4assert n % 2 == 0 : "odd: " + n;
0getstatic #7// Asserts.$assertionsDisabled : boolean
3ifne 26
6iload_0 // n
7iconst_2
8irem
9ifeq 26
12new #13// AssertionError
15dup
16iload_0 // n
17invokedynamic #15// makeConcatWithConstants(int) : String ⇐ StringConcatFactory.makeConcatWithConstants "odd: \1"
22invokespecial #19// new AssertionError(Object)
25athrow
5return n / 2;
26iload_0 // n
27iconst_2
28idiv
29ireturn

Open in the viewer

static { … }

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

2public class Asserts {
0ldc #8// Asserts
2invokevirtual #22// Class.desiredAssertionStatus() : boolean
5ifne 12
8iconst_1
9goto 13
12iconst_0
13putstatic #7// Asserts.$assertionsDisabled : boolean
16return

Open in the viewer

← Compile-time constantsAll topicsGenerics and bridge methods →

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.