HexplainedJavaThread dumpsHeap dumpsAbout
ViewerFormatWhat javac doesOpcodesConstantsAttributesVersionsJAR
Java class files › What javac does › Boolean logic and the ternary operator

What javac does

Boolean logic and the ternary operator

No boolean instructions: && and || become chains of jumps.

The JVM has no boolean type and no instructions for && or ||. A boolean expression compiles to branches that end at code pushing 1 (iconst_1) or 0 (iconst_0).

The source

// && and || become chains of jumps; there are no boolean instructions.
public class Conditions {
    static boolean inRange(int x, int lo, int hi) {
        return x >= lo && x < hi;
    }

    static String sign(int x) {
        return x > 0 ? "positive" : x < 0 ? "negative" : "zero";
    }

    static boolean either(boolean a, boolean b) {
        return a || b;
    }
}

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

What to look for

  • In x >= lo && x < hi, each comparison is inverted and jumps to the false result; falling through both pushes 1.
  • || is the mirror image: the first test that succeeds jumps straight to the true result.
  • The nested ternary in sign() becomes a chain of branches with gotos to a shared areturn.
  • The branches test the opposite of the source (ifle for x > 0) because javac jumps past the code for the true case.

Conditions.class

static boolean inRange(int x, int lo, int hi)

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

4return x >= lo && x < hi;
0iload_0 // x
1iload_1 // lo
2if_icmplt 14
5iload_0 // x
6iload_2 // hi
7if_icmpge 14
10iconst_1
11goto 15
14iconst_0
15ireturn

Open in the viewer

static String sign(int x)

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

8return x > 0 ? "positive" : x < 0 ? "negative" : "zero";
0iload_0 // x
1ifle 9
4ldc #7// "positive"
6goto 20
9iload_0 // x
10ifge 18
13ldc #9// "negative"
15goto 20
18ldc #11// "zero"
20areturn

Open in the viewer

static boolean either(boolean a, boolean b)

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

12return a || b;
0iload_0 // a
1ifne 8
4iload_1 // b
5ifeq 12
8iconst_1
9goto 13
12iconst_0
13ireturn

Open in the viewer

← Generics and bridge methodsAll topicsfor, while and do-while →

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.