I understand closure and have applied in some language such as Python and SML. Nevertheless, when I read wikipedia about closure in Java (of course, just 8 version), I don't understand difference if Java supports closure or not in their example.
Those code I copy from Wikipedia : Closure
The java code without closure :
class CalculationWindow extends JFrame {
private volatile int result;
...
public void calculateInSeparateThread(final URI uri) {
// The expression "new Runnable() { ... }" is an anonymous class implementing the 'Runnable' interface.
new Thread(
new Runnable() {
void run() {
// It can read final local variables:
calculate(uri);
// It can access private fields of the enclosing class:
result = result + 10;
}
}
).start();
}
}
And if Java supports closure, the code will looks like:
class CalculationWindow extends JFrame {
private volatile int result;
...
public void calculateInSeparateThread(final URI uri) {
// the code () -> { /* code */ } is a closure
new Thread(() -> {
calculate(uri);
result = result + 10;
}).start();
}
}
So, my question is : if Java supports closure, which special thing in second code ? I really don't see main difference between two code.
Please tell me this point.
thanks :)
The point is that they are not really so different functionally:
() -> {
calculate(uri);
result = result + 10;
}
is equivalent to a new class instance of Runnable with an equivalent implementation of the run() method. You replace a lot of "boiler-plate" code with a simple lambda function.
Using the lambda, your code becomes much more expressive, concise, easier to write, and very readable. And that's just where the benefits start, once you enter the world of closures and lambda functions.
The difference is that:
They achieve the same end, but the lambda syntax is certainly more light-weight.
However, a lambda can only access local variables in the scope in which it is declared if they are final
(or effectively final); see 15.27.2 in JSR-000335 review draft #2. So you could argue that Java lambdas are not full closures.
But the JSR-000335 spec does imply that lambdas are NOT anonymous classes. And this
and super
in a lambda body have a different meaning that they do in a method.
The spec describes lambdas (at one point) as being implemented using "synthetic classes", and states that an instance of the synthetic classes can be reused when appropriate as a compilation optimization. (By contrast, the compiler would not be allowed to make that optimization for an anonymous class.) This optimization means that lambdas are likely to perform better than the equivalent coded using anonymous classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With