What is the use of being able to use final variables effectively in Java 8 lambda expressions

I understand what “effectively final” means, as explained by the difference between final and effective final .

What I don't understand, why is it advisable to use efficient final variables for lambda expressions / annoying inner classes? Why will Java 8 loosen its limit on variables being declared final?

Does it just need to save the input finalbefore the variable? Or does the ability to use finite variables effectively have some other advantage?

+4
source share
2 answers

The simple answer is that there is no difference between finaland “effectively final” variables, in addition to the keyword finalin their declaration, the only goal is to exclude the keyword final.

But this can have a greater impact than you know.

For a lambda expression, the full declaration of the parameter (s) can be simplified, as in x -> x+1. Now consider the nested lambda expressions, such as: x -> y -> x+yand the visual clutter that will be created if we use to add an declaration finalto the parameter xhere.

Java final , , final x -> y -> x+y (final double x) -> y -> x+y.

, Java- ( ). , - , ( ), , final, , , API- , , - .

+14

Java, - . - lambdas , , , , .

+5

Source: https://habr.com/ru/post/1543975/


All Articles