New Early adopter or innovator? InfoQ has been working on some new features for you. Learn more

Java Lambda Syntax based on C#, Scala

| by Alex Blewitt on Sep 09, 2011. Estimated reading time: 2 minutes |

One of the most anticipated new features of Java 8 is the introduction of Lambdas, which are anonymous functions that can be passed around like data in arguments and variables. However, the syntax for these wasn't widely decided, with polls asking for input on what would be most familiar to others.

A recent posting on the lambda-dev mailing list announced the conclusion that the Java Lambda syntax will be based on C# syntax. This is already widely known and used (as C# had delegates back in 1.0, support for delegate functions in 2.0 and lambda expressions in 3.0).

Other alternatives were available (such as BGGA) but the response to the survey suggested that there wasn't a huge advantage of one over the other; in addition, the pre-existing experiences of those using C# and Scala mean that there are likely to be many more programmers familiar with the general type of syntax. (The key difference between the C# and the Scala style of anonymous function is with how arguments are typed; C# uses “int foo” whilst Scala uses “foo: int” – a difference that will be fairly easy to move between. Java will adopt C#'s style of “int foo” in deference to the way types are represented in Java.)

Despite extensive searching, there was no clear winner among the alternatives (each form had some good aspects and some really not very good aspects, and there was no form that was clearly better than the others). So, we felt that it was better to choose something that has already been shown to work well in the two languages that are most like Java – C# and Scala – rather than to invent something new.

Both expressions and blocks will be permitted. An expression is represented without the curly braces {} and will, when evaluated, return the result. Blocks of statements are surrounded with {} and will not return a result unless the return keyword is used. The examples given in the mail are:

x => x + 1
(x) => x + 1
(int x) => x + 1
(int x, int y) => x + y
(x, y) => x + y
(x, y) => { System.out.printf("%d + %d = %d%n", x, y, x+y); }
() => { System.out.println("I am a Runnable"); }

One key advantage of the lambda expressions is that they will infer the types of the arguments if not specified. In some cases, the compiler may not be able to infer the correct types (especially in the case of overloaded operators; the “(x,y) => x+y” is highly unlikely to compile if Java doesn't know whether the x and y are long or double (or even Object). In most cases the type inference engine is likely to generate the correct code, although if further assistance is needed then the developer can always explicitly add the typing information.

A compiler which understands the new syntax will be made available in the near future for experimentation purposes.

Rate this Article

Adoption Stage

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Juhu!!! by Lars Vogel

Great! Java builds on the shoulders of giants (C## and the little giant Scala). :-)

Amazing - Java-ites acknowledging other languages by Faisal Waris

Maybe they can also acknowledge the debt owed to Smalltalk while they are at it.

Curiously, a Smalltalk like language may be coming back as "Dart" from Google:

Re: Amazing - Java-ites acknowledging other languages by Mike Bria

We seriously have to wonder if this "Dart" thing is for real. I say tread with caution.

Re: Amazing - Java-ites acknowledging other languages by Diego Fernandez

Ey! Thanks for the link about Dart! It would be amazing if those guys make a Smalltalk like language sponsored by Google.
(coming from the smalltalk world, I know that the Self VM had wonderful things and the blog of Gilad Bracha about Newspeak always have interesting posts about programming languages and modularity)

Re: Amazing - Java-ites acknowledging other languages by Jörg W Mittag

Actually, the Newspeak-to-ECMAScript compiler has been "almost finished" for quite a while now. I suspect Dart is related to that, maybe a slightly modified version or a subset of Newspeak that is easier to compile to ECMAScript than the extremely dynamic and reflective "proper" Newspeak.

Newspeak has been designed from day 1 to be a web programming language, or rather to be the programming language for the next evolution of the web (one could even say that Newspeak was designed to be the next evolution of the web), so it would make sense. Even if you don't actually use all the cool revolutionary features of the full Newspeak system (which would be quite hard anyway, since they haven't been implemented yet :-) ), Newspeak is still a very nice object-oriented dynamic language with software-engineering capabilities far beyond those of your typical "scripting" languages (including Smalltalk), e.g. a proper module system. Plus, a very nice actor system (and web programming is basically always distributed, asynchronous programming which is exactly what actors do well) and powerful pattern matching (which, again, is a big part of what many web apps do).

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

5 Discuss

Login to InfoQ to interact with what matters most to you.

Recover your password...


Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.


More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.


Stay up-to-date

Set up your notifications and dont miss out on content that matters to you