Java 8 introduces Lambda expressions and supports anonymous inner classes to access local variables. Java 9 allows lambda expressions to capture local variables, and Java 10 implements local variable type inference, where the compiler can infer lambda expression parameters and return types. Practical cases show the impact of lambda expressions on local variable access and type inference in different Java versions.
The impact of Java version on Lambda expression
Introduction
Lambda expression Formats are a key feature introduced in Java 8 that allow us to declare functions as instances of objects. Before Java 8, we could only use anonymous inner classes to implement functions.
Java 8
In Java 8, Lambda expressions are represented by the arrow symbol (->), which separates the input parameters from the function body. For example:
(x) -> x + 1
This Lambda expression takes an int input parameter and returns x 1.
Java 9
Java 9 introduces support for local variable capture for Lambda expressions. This means that lambda expressions can now access local variables in their scope, even if they are not of final type. For example:
int y = 5; (x) -> x + y
This Lambda expression can access the local variable y even though it is not of final type.
Java 10
Java 10 introduces support for local variable type inference for lambda expressions. This means that the compiler can infer the types of lambda expression parameters and return types without us explicitly declaring them. For example:
(x, y) -> x + y
This Lambda expression can infer that the input parameters are of type int and the return type is of type int, even though we did not declare them explicitly.
Practical case
The following is a practical case showing the impact of different Java versions on Lambda expressions:
// Java 8 public static void main(String[] args) { int y = 5; // 匿名内部类 Runnable runnable = new Runnable() { @Override public void run() { System.out.println(y); // 无法访问 } }; // Lambda 表达式 Runnable lambda = () -> System.out.println(y); // 无法访问 }
In Java 8, anonymous internals The class cannot access local variables y in scope because they are not of final type. Lambda expressions also cannot access y because local variable capture is not supported in this version of Java.
// Java 9 public static void main(String[] args) { int y = 5; // 匿名内部类 Runnable runnable = new Runnable() { @Override public void run() { System.out.println(y); // 可以访问 } }; // Lambda 表达式 Runnable lambda = () -> System.out.println(y); // 可以访问 }
In Java 9, both anonymous inner classes and Lambda expressions can now access local variables y in the scope since support for local variable capture was implemented.
// Java 10 public static void main(String[] args) { int y = 5; // Lambda 表达式,自动推断类型 Runnable lambda = (x) -> { System.out.println(x + y); }; }
In Java 10, Lambda expressions can automatically infer the types of their input parameters and return types. Therefore, we do not have to explicitly declare the types of input parameters and return types in the lambda expression.
The above is the detailed content of The impact of different Java versions on functions. For more information, please follow other related articles on the PHP Chinese website!