Sun’s Compiler and Runtime Optimizations
As you can see from the previous sections, knowing how the compiler alters your code as it generates bytecodes is important for performance tuning. Some compiler optimizations can be canceled out if you write your code so that the compiler cannot apply its optimizations. In this section, I cover what you need to know to get the most out of the compilation stage if you are using the JDK compiler ( javac ).
Optimizations You Get for Free
There are several optimizations that occur at the compilation stage without your needing to specify any compilation options. These optimizations are not necessarily required because of specifications laid down in Java. Instead, they have become standard compiler optimizations. The JDK compiler always applies them, and consequently almost every other compiler applies them as well. You should always determine exactly what your specific compiler optimizes as standard, from the documentation provided or by decompiling example code.
Literal constants are folded
This optimization is a concrete implementation of the ideas discussed in Section 3.4.2.5 earlier. In this implementation, multiple literal constants [21] in an expression are “folded” by the compiler. For example, in the following statement:
int foo = 9*10;
the 9*10 is evaluated to 90
before compilation is completed. The result is as if the line read:
int foo = 90;
This optimization allows you to make your code more readable without having to worry about avoiding ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access