HexplainedJavaThread dumpsHeap dumpsAbout
ViewerFormatWhat javac doesOpcodesConstantsAttributesVersionsJAR
Java class files › What javac does › Comparing longs, floats and doubles

What javac does

Comparing longs, floats and doubles

A compare instruction pushes -1, 0 or 1; then an ordinary int branch.

There is no "branch if long less than". Comparing longs, floats or doubles takes two instructions: a compare that pushes -1, 0 or 1, then an int branch on the result.

The source

// Comparing longs, floats and doubles: a compare instruction first, then an int branch.
public class Compare {
    static boolean longLess(long a, long b) {
        return a < b;
    }

    static boolean floatLess(float a, float b) {
        return a < b;
    }

    static boolean doubleGreater(double a, double 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

  • longLess is lcmp, then ifge to the false result.
  • floatLess uses fcmpg and doubleGreater uses dcmpl. The two forms differ only in what NaN gives, and javac picks the one that makes any comparison with NaN false.

Compare.class

static boolean longLess(long a, long b)

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

4return a < b;
0lload_0 // a
1lload_2 // b
2lcmp
3ifge 10
6iconst_1
7goto 11
10iconst_0
11ireturn

Open in the viewer

static boolean floatLess(float a, float b)

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

8return a < b;
0fload_0 // a
1fload_1 // b
2fcmpg
3ifge 10
6iconst_1
7goto 11
10iconst_0
11ireturn

Open in the viewer

static boolean doubleGreater(double a, double b)

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

12return a > b;
0dload_0 // a
1dload_2 // b
2dcmpl
3ifle 10
6iconst_1
7goto 11
10iconst_0
11ireturn

Open in the viewer

← VarargsAll topicsx++ and friends →

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.