29

I have heard that closures could be introduced in the next Java standard that is scheduled to be released somewhere around next summer.

What would this syntax look like?

I read somewhere that introducing closures in java is a bigger change than generic was in java 5. Is this true? pros and cons?

(By now we definitely know that closures not will be included in the next Java release)

OR

edit: http://puredanger.com/tech/2009/11/18/closures-after-all/ :D

edit2: Re-thinking JDK7: http://blogs.oracle.com/mr/entry/rethinking_jdk7

edit3: There’s not a moment to lose!: http://blogs.oracle.com/mr/entry/quartet

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
Schildmeijer
  • 20,702
  • 12
  • 62
  • 79
  • 14
    Right now I'm more interested in Scala than in whatever new language features Java might have in the near future - Scala et al. already has them and they are better tied into the language than Java can ever be. I think it's time to move on to newer languages with less historical baggage. – Esko Luontola May 04 '09 at 23:52
  • 9
    Advancement of the JVM as a platform for other languages is more important. Reified generics would be a blessing. – Esko Luontola May 04 '09 at 23:53
  • 8
    @Esko: you move onto your new languages - I'd like the keep the massive investment I've already made in mine. :-) – Chris K May 10 '10 at 23:33
  • 4
    Scala interops fine with Java. So you could keep your investments and enhance your productivity with a better language. – Brian DiCasa Oct 21 '11 at 15:10

9 Answers9

17

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.

Steven Huwig
  • 20,015
  • 9
  • 55
  • 79
13

It really depends on what gets introduced, and indeed whether it will be introduced at all. There are a number of closure proposals of varying sizes.

See Alex Miller's Java 7 page for the proposals and various blog posts.

Personally I'd love to see closures - they're beautiful and incredibly helpful - but I fear that some of the proposals are pretty hairy.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
11

In November 2009 there was a surprising u-turn on this issue, and closures will now be added to Java 7.

Update

Closures (AKA lambdas expressions) in Java 7 didn't happen. They were finally added in the first release of Java 8 in 2014.

Dónal
  • 185,044
  • 174
  • 569
  • 824
  • 1
    That is what I've heard as well. Hence the small language changes (aka Project Coin - http://openjdk.java.net/projects/coin/) project. – Brian Yarger May 04 '09 at 23:29
  • 1
    Project Coin isn't related to closures. You can't call closures a small change, right? – Jorn Jan 11 '10 at 18:04
4

Unofortunately you will not find closure in Java 7. If you are looking for a lighter solution to have closure in java just now check out the lambdaj project:

http://code.google.com/p/lambdaj/

Dónal
  • 185,044
  • 174
  • 569
  • 824
Mario Fusco
  • 13,548
  • 3
  • 28
  • 37
  • 2
    Thank you for the misinformation, Mario. First, lambda expressions are currently slated for Java 7. Second, lambdaj does not provide closures, as it cannot capture variables from the lexically enclosing scope. – Neal Gafter Jul 07 '10 at 14:56
  • 1
    Hi Neal. If you read the date of my answer it has been posted on September 2009. That means it was 2 months before the announcement of closure in Java 7 at Devoxx. If you remember at that time practically everyone was sure that we won't have closure in Java 7. Do you agree? What should I did after the announcement? Look all over the internet for all the posts where I spoke about closure in Java 7 to correct them? – Mario Fusco Jul 07 '10 at 16:53
  • 1
    Good point; my mistake. So this answer is not incorrect with respect to the Java 7 status, merely out of date. As for lambdaj, a presumably very useful library, it does provide something like lambda expressions but not closures. – Neal Gafter Jul 07 '10 at 17:58
3

This is the java 7 features http://tech.puredanger.com/java7/#switch the examples are very usefull.

Agusti-N
  • 3,956
  • 10
  • 40
  • 47
3

Note that a "function-type" is really a type under the proposal:

{int => boolean} evaluateInt;    //declare variable of "function" type
evaluateInt = {int x => x % 2 }; //assignment
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
2

I think there is still a lot of debate going in with regards to what syntax will ultimately be used. I'd actually be pretty surprised if this does make it into Java 7 due to all of that.

jsight
  • 27,819
  • 25
  • 107
  • 140
2

closures will be annoyinglly verbose if there won't be any sort of type inference... :(

Pedro Rolo
  • 28,273
  • 12
  • 60
  • 94
1

Closures have some serious edge cases. I would say that Closures are a much more significant change than Generics and the later still has a number hairy edge cases. e.g. The Java Collections libraries cannot be written/compiled without warnings.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    I'd say that's more of a problem with Java's implementation of generics than with the concept of generics itself. – bcat Nov 19 '09 at 21:03
  • 1
    I agree. It now appears that a simplified form of Closures will be in a very delayed Java 7. – Peter Lawrey Nov 20 '09 at 22:41