Object – Functional programming

We like it or not, it seems that multi-paradigm programming is growing up rapidly!

A good example is Scala : an object-functional programming and scripting language for general software applications, statically typed, designed to concisely express solutions in an elegant, type-safe and lightweight manner. Scala includes full support for functional programming (including currying, pattern matching, algebraic data types, lazy evaluation, tail recursion, immutability, etc.).

But, there are good news for java developers in this manner . With the release of JDK 8 planned for 2013, Oracle has a pretty fixed idea of what will be included. Speaking at QCon London earlier this year, Simon Ritter outlined the new features that will be part of JDK 8, which include modularity (Project Jigsaw), JRockit/Hotspot convergence, annotations on types, and Project Lambda.

JSR 335 (Lambda Expressions for the JavaTM Programming Language) aims to support programming in a multicore environment by adding closures and related features to the Java language.

JSR 335 is part of JSR 337 (Java SE 8).

The goal of OpenJDK Project Lambda is to host a prototype implementation of JSR 335 suitable for inclusion in JDK 8, the Reference Implementation of Java SE 8. The prototype implementation is discussed on the lambda-dev mailing list.

A common use of lambda expressions is in GUI development. In general, GUI programming resolves around connecting behaviour to events. For example, if a user presses a button (an event), your program needs to execute some behaviour. This might be the storage of some data in a datastore. In Swing, for example, this is done using  ActionListeners  by using anonymous inner classes :

class UIBuilder {
public UIBuilder() {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//do something
}
}
}
}

Hello Lambda Expressions, Goodbye Anonymous Inner Classes
Let’s see how lambda expressions and type inference help us to simplify our class:

class UIBuilder {
public UIBuilder() {
button.addActionListener(e -> //process ActionEvent e)
}
}

Instead of defining a class to hold our callback method, we now directly pass a lambda expression into the addActionListener method. Besides the reduction lot of boilerplate code and increased readability, it lets us directly express the only thing we are really interested in: handling the event.
This example just shows us a very simple usage of lambda.