Closure in Java 7

Closure in Java 7

Here is Neal Gafter's blog one of the pioneers introducing closures in Java. His post on closures from January 28, 2007 is named A Definition of Closures On his blog there is lots of information to get you started as well as videos. An here is an excellent Google talk - Advanced Topics In Programming Languages - Closures For Java with Neal Gafter, as well.

Closures in Java 7

Have a look at http://www.javac.info/ .

It seems like this is how it would look:

boolean even = { int x => x % 2 == 0 }.invoke(15);

where the { int x => x % 2 == 0 } bit is the closure.

How do closures work in Java 7?

Java 7 has no closures. They've been rumored for a long time, and they are apparently set to appear in Java 8. Of course, I have been promised a Ghostbusters reboot that has been rumored too.

However, you can fake closures with anonymous inner classes. But make no mistake, these aren't closures.

As for the benefits of closures, I can't put it any better than Stack Overflow legend @jaif from this post:

"You can see it as a generalization of a class.

Your class holds some state. It has some member variables that its methods can use.

A closure is simply a more convenient way to give a function access to local state.

Rather than having to create a class which knows about the local variable you want the function to use, you can simply define the function on the spot, and it can implicitly access every variable that is currently visible.

When you define a member method in a traditional OOP language, its closure is "all the members visible in this class".

Languages with "proper" closure support simply generalize this, so a function's closure is "all the variables visible here". If "here" is a class, then you have a traditional class method.

If "here" is inside another function, then you have what functional programmers think of as a closure. Your function can now access anything that was visible in the parent function.

So it's just a generalization, removing the silly restriction that "functions can only be defined inside classes", but keeping the idea that "functions can see whatever variables are visible at the point where they're declared". "

What is a closure? Does java have closures?

A closure is a first class function with bound variables.

Roughly that means that:

  • You can pass the closure as a parameter to other functions
  • The closure stores the value of some variables from the lexical scope that existed at the time that is was created

Java initially didn't have syntactic support for closures (these were introduced in Java 8), although it was fairly common practice to simulate them using anonymous inner classes. Here's an example:

import java.util.Arrays;
import java.util.Comparator;

public class StupidComparator {
public static void main(String[] args) {
// this is a value used (bound) by the inner class
// note that it needs to be "final"
final int numberToCompareTo=10;

// this is an inner class that acts like a closure and uses one bound value
Comparator<Integer> comp=new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
int result=0;
if (a<numberToCompareTo) result=result-1;
if (b<numberToCompareTo) result=result+1;
return result;
}
};

Integer[] array=new Integer[] {1,10, 5 , 15, 6 , 20, 21, 3, 7};

// this is a function call that takes the inner class "closure" as a parameter
Arrays.sort(array,comp);

for (int i:array) System.out.println(i);
}
}

Using Java 7 closure syntax in Eclipse 3.7.1

Java 7 doesn't have closures - it's a planned feature for Java 8.

It was hoped that it would be in Java 7, but it didn't make the cut.

What’s the current state of closures in Java?

At Devoxx 2008, Mark Reinhold made it clear that closures will not be included in Java 7.


Wait! Closures will be included in Java 7. Mark Reinhold announced this reversal at Devoxx 2009.


Belay that! Closures (lambda expressions) have been deferred until Java 8. Follow Project Lambda (JSR 335) for more information.

Why is Java Lambda also called Closures

Those are two different terms that happen to often be mentioned in the same context.

  • A lambda is basically just an anonymous function. Example: () -> System.out.println("Hello"). It's a function, but it doesn't have a name.

  • A closure is a term regarding scoping. When you for instance refer to a local variable inside a lambda, as follows

    int localInt = 17;

    saveThisFunction(() -> System.out.println(localInt));

    you create a closure to capture localInt inside the lambda. Textually it looks obvious that it should be ok to access localInt inside the lambda, but keep in mind that the lambda can be stored and called long after the localInt has been popped from the stack.

So, creating a lambda expression often requires creating a closure (implicitly).



Related Topics



Leave a reply



Submit