Short answer: it depends.
If you are completely limited to Oracle javac and libraries, the answer is: no; the following reasons.
Java bytecode contains the major version number. By default, the Java 8 compiler puts java8 in the data. Any older JVM simply refuses to run this code. Perhaps, although you can tell the Java 8 compiler to create bytecode compatible with older JVMs. But: in order for the older JVM to execute such โspecialโ class files, you need all to make its dependencies available!
And there it breaks: Lambdas uses the invokedynamic bytecode instruction, which does not exist in Java 6. And besides this, the compiler uses a lot of Java library materials when compiling lambdas - they are all added after java 6.
So, even if you manage to compile lambda using Java 6 bytecode source code, this instruction is not available, and you also need to provide all these other classes.
But: as another excellent answer explains, there are javac alternatives that allow you to use lambda on older JVMs.
But: be careful how to spend your energy. Java 6 is still dead for server-side java. Thus, the use of these alternatives is suitable for platforms such as Android, but when you are still using the Oracle Java 6 JVM, you should spend your energy upgrading this system to the current version of Java.
GhostCat Apr 23 '17 at 8:56 on 2017-04-23 08:56
source share