Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can newer Java version (9, 10, 11) be compiled to run on older platforms (e.g Java 8)?

Is it possible to develop using newer Java (e.g 9, 10, 11 etc.) versions, and have that Java be compiled down to some older version - such as Java 8?

We would like to use the new features of newer Java versions, but our code needs to run on a Java 8 platform.

If it matters: we are building with Maven.


Phrasing it in Javascript fashion:

Does there exist a Babel Polyfill equivalence for Java?

like image 378
Tormod Haugene Avatar asked Dec 14 '22 12:12

Tormod Haugene


2 Answers

No - you cannot use new features on an older JVM; the bytecode is incompatible.

You can absolutely use the newer versions of Java to compile down to a lower version, with some caveats around modules.

If you need the new features of newer Java versions, then it's time to migrate to a new version; other than that, where you are will be fine until you require support from Oracle.

like image 56
Makoto Avatar answered Dec 28 '22 07:12

Makoto


Not necessarily. If you have code that uses packages/syntactical structures not defined in previous versions, the program will fail to run/compile.

Think of it this way. Before Java 8, there were not lambda expressions. So if you create a lambda expression in Java 8+ and then attempt to compile it in java 7, the compiler will be like: What is a lambda expression?

In short no. However, you can go the other way. Compile code written in a lower version using a higher version... for the most part.

like image 40
JustAFellowCoder Avatar answered Dec 28 '22 06:12

JustAFellowCoder