1

Would the java compiler (any mainstream compiler) take something like

void foo () {
    int x = 1;
    System.out.println(x);
}

void bar() {
    foo()
}

and optimize the foo method away if it is only called in bar resulting in bytecode that would also be generated by this:

void bar() {
    int x = 1;
    System.out.println(x);
}
Bren
  • 3,516
  • 11
  • 41
  • 73

1 Answers1

2

No it doesn't. Javac doesn't do much optimization beyond constant folding. In java the optimization happens at runtime with the JIT.

I created a short example to show how the method did not get inlined in the bytecode even though it was only called once. Private and public make no difference.

Compiled from "Test.java"
public class Test {
  public Test();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #3                  // String Hello before method
       5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: invokestatic  #5                  // Method publicMethod:()V
      11: invokestatic  #6                  // Method privateMethod:()V
      14: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
      17: ldc           #7                  // String Hello after method
      19: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      22: return

  public static void publicMethod();
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #8                  // String The public method
       5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return

  private static void privateMethod();
    Code:
       0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #9                  // String The private method
       5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return
}
puhlen
  • 8,400
  • 1
  • 16
  • 31