2

I'm about to upgrade a tomcat installation from version 6.0 to version 8.5, and also upgrading the JVM from 6 to 8. Our java code is compiled with java 6 as for now.

I know that running java code on the latest JVM is always a good idea performance-wise (not to mention security-wise). In this way some code compiled with java 1.6 might run faster on JVM 8 compared to JVM 6.

But how about compilation? Does one gain anything by compiling code written in java 6 with a java 8 compiler (i.e. only java 6 features are used)? I.e. does code written with the intent of compiling with java 1.6 run faster on JVM 8 when the code is compiled with java 8 (targeted 8) and not java 6? Is the bytecode from java 8 optimized compared to java 6?

And regarding the target flag. Is anything gained by compiling java 6 code with a java 8 compiler targeted to java 6 (when the code is still to be run on JVM 6), compared to if the code was compiled with a java 6 compiler?

1
  • 1
    Java is compile, once, run anywhere. Once it's compiled, you don't need to re-compile it to take advantage of the latest JVM. You can take JAR for version 1.0.2 and run it on a Java 9 JVM and it will use all the latest optimisations. Commented Jan 7, 2017 at 9:35

2 Answers 2

2

Is anything gained by compiling java 6 code with a java 8 compiler targeted to java 6 compared to if the code was compiled with a java 6 compiler?

No.

There is absolutely no performance gain, since the java compiler don't perform any kind of optimization, not even obvious ones.

Any optimization that could be performed on the bytecode produced by javac is done by the JIT engine once your application is running.

Edit: Look at pveentjer comment, a few simple optimizations (e.g. constant resolution) are performed by javac. But i'm still confident that the answer regarding the differences between 6 and 8 is still "no".

Sign up to request clarification or add additional context in comments.

2 Comments

There are not many optimizations applied by the Javac; but there are some: pveentjer.blogspot.com.tr/2017/01/final-static-boolean-jit.html
I've never noticed that, I'm wondering when it was introduced.
0

As you already know, there is no real requirement for compiling Java code with the latest versions of the language. It is up to you.

Common points to consider:

  • Compatibility issues with dependency libraries and frameworks
  • Compatibility issues/unsupported binary versions on the target JVM versions
  • Known performance (or other improvements) in the newer Java versions.
  • Backward compatibility - code compiled with newer Java (e.g. 8) might not always be able to execute on older JVM (e.g. 6).

I would (and I encourage you) to check the release notes to see what changes, improvements, compatibility constraints etc. are introduced in the new JVM version.

1 Comment

Hi @Ivan: Thanks for your reply. I know it is not required to upgrade the compiler, but is there any performance gain?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.