Method Inlining

Definition

Inlining is an optimization performed by the Java Just-In-Time compiler.

public int addPlusOne(int a, int b){
   return a + b + 1;
}

public void testAddPlusOne(){
   int v1 = addPlusOne(2, 5);
   int v2 = addPlusOne(7,13);
}

The compiler may decide to replace your function call with the body of the function, so the result would effectively look like this

public void testAddPlusOne(){
   int v1 = 2 + 5 + 1;
   int v2 = 7 + 13 + 1;
}
  • The compiler does this to save the overhead of actually making a function call, which would involve pushing each parameter on to the stack.
  • This can be done for non-virtual functions because you won’t know which code it contains if virtual function is overridden by others until runtime. Since java methods are virtual by default. You can make methods static or final to make non-virtual.

Leave A Comment

Your email address will not be published. Required fields are marked *