Literal constants are folded String concatenation is sometimes folded Constant fields are inlined

- 69 - 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 .

3.5.1 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.

3.5.1.1 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 [7] in an expression are folded by the compiler. For example, in the following statement: [7] Literals are data items that can be identified as numbers, double-quoted strings, and characters, e.g., 3, 44.5e-22F, 0xffee, h, hello, etc. int foo = 910; the 910 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 runtime overheads.

3.5.1.2 String concatenation is sometimes folded

With the Java 2 compiler, string concatenations to literal constants are folded: String foo = hi Joe + 910; is compiled as if it read: String foo = hi Joe 90; This optimization is not applied with JDK compilers prior to JDK 1.2. Some non-Sun compilers apply this optimization and some dont. The optimization applies where the statement can be resolved into literal constants concatenated with a literal string using the + concatenation operator. This optimization also applies to concatenation of two strings. In this last case, all compilers fold the two or more strings, as that action is required by the Java specification.

3.5.1.3 Constant fields are inlined

Primitive constant fields those primitive data type fields defined with the final modifier are inlined within a class and across classes, regardless of whether the classes are compiled in the same pass. For example, if class A has a public static final field, and class B has a reference to this field, the value from class A is inserted directly into class B , rather than a reference to the field in - 70 - class A . Strictly speaking, this is not an optimization, as the Java specification requires constant fields to be inlined. Nevertheless, knowing about it means you can take advantage of it. For instance, if class A is defined as: public class A { public static final int VALUE = 33; } and class B is defined as: public class B { static int VALUE2 = A.VALUE; } then when class B is compiled, whether or not in a compilation pass of its own, it actually ends up as if it was defined as: public class B { static int VALUE2 = 33; } with no reference left to class A .

3.5.1.4 Dead code branches are eliminated