0

Can I be sure that

private int value;
public int getValue() { return value; }

is compiled by compiler in the same way as

public int Value;

in the terms of number of instructions and execution time? I mean do modern compilers trying to make functions "inline" (c++ term)?

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385

1 Answers1

4

No; the Java compiler will not change that.

However, the JITter probably will.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Indeed, the JIT optimizes it for you. – Gilberto Torrezan Oct 22 '12 at 15:41
  • @Gilberto Won't the JIT only optimize it if it discovers a need to? I'm not comfortable asserting a JIT will always optimize a "simple" getter (less so a more complex on) given there are multiple implementations and environments. – Dave Newton Oct 22 '12 at 15:44
  • 6
    @DaveNewton Programming in Java is an exercise of faith: you hope the GC will run, you hope the JIT will compile and optimize, you hope you write once and the program will run everywhere. Have faith, brother. ;-) – Gilberto Torrezan Oct 22 '12 at 15:47
  • I was thinking JIT is a compiling too. – Suzan Cioc Oct 22 '12 at 16:26