Monday, November 14, 2011

OpenJDK Java8 Lambda Syntax

This document provides some working examples of the future OpenJDK 8 lambda syntax.   There was a lot of discussion whether the language changes would get included in the Java7 or Java8 so it looks like we will see the changes in Java8 slated for release in 2013.

The lambda conversion uses a target SAM type or single abstract method.

You can see the conversion in this example:

    interface F {
        void f();
    }


        final F func = () -> System.out.println("Test");
        System.out.println("Function Object : " + func);
        func.f();
...
The variable type on the left is an interface of type 'F' with a single method 'f'.

It seems that the Java lambda implementation provides a slightly more expressive syntax for code blocks that you would normally see in an anonymous inner class.  These sections of  code are synonymous.

        for (int i = 0; i < 10; i++) {
            final int ii = i;
            final int res6 = ((Fx)( (final int y) -> y + ii )).f(10);          
            System.out.println("[6] Output from recent lambda call index="+i+" : " + res6);
        }
        ...

        for (int i = 0; i < 10; i++) {
            final int ii = i;
            // @line70, anon inner class call
            final Fx fxx = new Fx() {
              public int f(int y) { return y + ii; }
            };
            final int res6 = fxx.f(10);          
            System.out.println("[7] Output from anonymous inner call index="+i+" : " + res6);
        }


Resources:


The Lambda team provided a binary build of Javac and Java for Windows and Linux.

[1] http://jdk8.java.net/lambda/


$ ./java.bat
openjdk version "1.8.0-ea"
OpenJDK Runtime Environment (build 1.8.0-ea-b1314)
OpenJDK Client VM (build 23.0-b04, mixed mode)

https://github.com/berlinbrown/BuildingOpenJDKCookbook

BuildingJDK8/ExampleLambdaProject/LambdaJava8Test.java


http://hg.openjdk.java.net/lambda/lambda/

http://mail.openjdk.java.net/pipermail/lambda-dev/attachments/20100122/3764c21a/attachment.txt

----

The Thoracic system may be compared to a great freight system, with each of its tributaries[Pg 218]—from the main trunk arteries down to the tiniest blood vessels—starting from the heart and carrying its cargo of blood to every part of the body by means of the power furnished by the lungs.



No comments: