Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are compiled Java 8 lambda expressions backwards compatible with earlier versions of the Java runtime?

In order to reduce the clutter caused by numerous instantiations of anonymous types, I'm exploring the possibility of leveraging Java 8 lambdas.

One important consideration before using Java 8 and lambdas in a production environment is whether JDK8-compiled code that uses lambda expressions can be executed on an earlier version of the Java runtime. I'm specifically interested in JRE6 and JRE7 as target platforms.

One one hand, I understand that lambdas are simply syntactic sugar around an instantiation of an anonymous class containing one method. On the other hand, I'm not certain that this equivalence implies that the bytecode generated for each is identical and/or compatible across JVM versions other than JRE8.

For example, given the single-method interface:

public interface Action<T> {
    public void perform(T argument);
}

The following two snippets are "functionally" equivalent:

With lambda:

final Action<String> y = i -> System.out.println(i);

With anonymous class instance:

final Action<String> y = new Action<String>() {
    @Override
    public void perform(final String i) {
        System.out.println(i);
    }
};

My specific question is whether the semantic equivalence of both constructs extends to equivalence of their compiled representations. Furthermore, if they indeed compile equivalently, does this equivalence indicate that the compiled form of a lambda expression can be hosted on earlier versions of the Java runtime without modification?

like image 582
bnu Avatar asked Mar 22 '14 01:03

bnu


People also ask

Is Java 8 backwards compatible?

Java SE 8 is strongly compatible with previous versions of the Java platform. Almost all existing programs should run on Java SE 8 without modification.

Is Jdk 17 backwards compatible with JDK 8?

JDK 8, 11, and 17 are all reasonable choices both for compiling and running Scala code. Since the JVM is normally backwards compatible, it is usually safe to use a newer JVM for running your code than the one it was compiled on, especially if you are not using JVM features designated “experimental” or “unsafe”.

Is Java still backwards compatible?

Java (VM) is Backward compatible. Code built by java 1.4. 2 will run on 1.5 & 6 VM's. The JDK compiler is not backward compatible.

Is Java 8 lambda expression correct?

Q 6 - Which of the following is correct about Java 8 lambda expression? A - Using lambda expression, you can refer to final variable or effectively final variable (which is assigned only once).


2 Answers

In general it is not possible to have Javac compiler use a source level that is higher than the target JVM level. Thus the answer is NO.

like image 61
anttix Avatar answered Oct 01 '22 12:10

anttix


Officially no, but for an unofficial solution you should look at the Retrolambda project. It doesn't backport the Collection API changes, but it can handle lambda expressions (and method references) for you.

like image 24
seanf Avatar answered Oct 01 '22 13:10

seanf