HOWTO · Java

Java Method Inlining: How the JVM Optimizes Calls

Learn why Java has no inline-function declaration, how HotSpot may inline method calls, and how to compare equivalent Java code.

On this page

Java has no source-level inline function declaration. When developers search for a Java inline function, they usually mean method inlining: a runtime optimization in which a JVM may replace a method call with the method’s instructions. You write ordinary Java methods; the JVM decides whether to inline calls while running the program.

What Is Method Inlining in Java?

A method call normally transfers control to the method and returns its result. Inlining can remove that call in compiled machine code by placing the method’s instructions at the call site. This is a JVM optimization, not a change to your Java source and not something you perform by copying code yourself. The Java compiler still compiles the source into class files, and the JVM remains responsible for runtime execution and optimization.

Inlining also does not mean that every method is expanded everywhere. The runtime must preserve the program’s behavior, and it may keep a call when inlining is unsuitable. Keep methods clear and let the JVM make this implementation choice.

How the HotSpot JVM Performs Method Inlining

HotSpot’s just-in-time (JIT) compiler can inline eligible method calls as part of runtime optimization. The decision depends on the code and runtime conditions; it is not a Java language guarantee. Therefore, do not describe inlining as always enabled in every JVM, or rely on an implementation-specific flag to make this example work. The example below demonstrates equivalent calculations, but it does not control or reveal what HotSpot compiles internally.

Build a Java Example That Compares a Method Call With Equivalent Statements

The first calculation calls addBonus and then score. The second performs the same arithmetic directly. Both start from the same value, so their results can be compared without claiming the JVM actually inlined either call.

Run this example:

public class Main {
    static int addBonus(int score) {
        return score + 2;
    }

    static int score(int value) {
        return value * 3;
    }

    public static void main(String[] args) {
        int byMethodCalls = score(addBonus(4));

        int value = 4;
        int byStatements = (value + 2) * 3;

        System.out.println("Method calls: " + byMethodCalls);
        System.out.println("Direct statements: " + byStatements);
    }
}
Method calls: 18
Direct statements: 18

Compile and Run the Example

Save the source as Main.java in an empty working directory. From that directory, compile and run it with a JDK on your PATH:

Run javac Main.java and then java Main from that directory. These commands use the class name and filename shown above and require no JVM tuning options. The supplied run of this exact example exited successfully and printed the output shown above.

What the Results Do and Do Not Show

When you run the program, compare the two printed values: they should match because the method calls and direct expression use the same arithmetic. That establishes the result equivalence of this small example. It does not show whether the JVM inlined either method. Matching values also say nothing about which form runs faster; measuring performance would require a separate, controlled benchmark. In short, use ordinary Java methods for readable source, and treat inlining as a possible runtime optimization rather than a feature you declare or a performance result you can infer from this output.