javaperformanceoptimizationruntime

do java classes compiled with backward compatability use JavaVM optimalization of newer versions?


I was wondering if, when compiling a java source that is written for java 1.4 with -source and -target switches set to 1.4, will use some/any of the optimizations built into the versions in between. My first intention is to say of course not, since after compiling you get a set of instructions that have as target the original 1.4 VM. Then i thought that since the compiler simply makes smarter use of the instructions there should still be some improvement.

Or to put it in another way do the most optimizations sit in the VM that runs the code or the compiled classes?

Please save the comments with 1.4 being hopelessly outdated, the question is simply something that i thought about for two days now and searching the internet has not provided me with any reasonably good answers to that question.


Solution

  • It is a common misconception that the javac compiler optimises code. It doesn't in 99% of cases and produces mostly a literal translation of what you wrote as byte code.

    All the optimisations are in the JIT at runtime. This means code compiled for JDK 1.0 will runs on the latest processors be as effected as the same code complied with the latest update of Java 7 or 8.

    Note: the compiler does a small amount of optimisation in calculating constants known at compile time. e.g. 1+1 => 2 and "hello " + "world" => "hello world". These optimisations haven't changed much over the years and they were there in Java 1.2, possibly earlier.

    compiling you get a set of instructions that have as target the original 1.4 VM

    Only one byte code instruction was added in Java 7 since Java 1.0 and Java doesn't use it. It was added to support dynamic languages running on the JVM.

    do the most optimizations sit in the VM that runs the code or the compiled classes

    You can assume it does. Java 1.0 - 1.4 will use StringBuffer for string concatenation whereas Java 5.0+ will use the slightly optimised StringBuilder. The difference is pretty small (but measurable)

    Please save the comments with 1.4 being hopelessly outdated,

    Java 6 is already out dated, and Java 8 will make Java 7 look very out dated (when it eventually comes out sometime this year)