September 18, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Guide to Lambda Expression in Eclipse Luna

  • July 30, 2014
  • By Manoj Debnath
  • Send Email »
  • More Articles »

The recent release of Eclipse Luna is ripe for Lambda Expression and has the full support of Java 8 in its development tools stack apart from several other enhancements. The inclusion of Lambdas in Java is undoubtedly the flagship feature of its latest release. There has been a heated discussion of its pros and cons, but ultimately it crept into the Java programming language (JSR 335). After Netbeans, Eclipse Luna joined the bandwagon as the full featured supporting IDE for lambda expression. This enhancement obviously was inevitable as well as expected from the Eclipse community. The programmer’s take on this is that we can now take up our favorite IDE to experiment with lambda expression in between doing plain old Java code. The article focuses on getting started with Lambdas with code snippets using Eclipse Luna as the IDE.

Glimpse on the Background

Lambda also known as Closure has its origin from Lambda Calculus. Lambda Calculus is the basis of functional programming. The intricacies that we encounter as we dive deeper into the background of Lambdas get a little complicated, but I believe it’s necessary to excite one's inquisitive mind; a thread to do your own R&D. Anyway, to make things simpler and give a glimpse – every programming language falls into one or more programming paradigms such as declarative language, imperative language, functional language, etc. Traditional programming languages are imperative in nature, in the sense that we provide some sequence of instruction to the computer to perform some action like - do this, do that, loop five times, etc.

<instruction 1>...;
<instruction 2>...;
...
<instruction N>...;

Functional programming on the other hand is an approach based on function calls construct. That means we do not explicitly provide a sequence of commands but implicitly instructions are executed to get some desired result.

<func1>(<func2>(<func3>...)...))

These nested function calls receive value from the called function and pass value to the calling function. This is just a scratch to give an idea; there is more to it in classical texts than meets the eye, especially its mathematical counterpart Lambda calculus. Interested readers may go through Lambda Calculus to appreciate Functional programming better.

Configuring Eclipse Luna

Eclipse can be configured to use Java 8 features from Window → Preference, and then select sub section Compiler from the Java section in the left pane. Select 1.8 to Compiler compliance level in the JDK compliance box. Apply → OK. That's it.

Note: This can be set in Eclipse Kepler, the predecessor of Luna as well, provided we are using jdk 8, otherwise there is no sense in setting the compliance to 1.8. What sets apart Luna from Kepler is that, Luna is full featured Java 8 compliant along with a huge stack of enhancements. Interested readers may compare them in Eclipse Kepler highlights and Eclipse Luna highlights.

Eclipse can be configured to use Java 8 features
Eclipse can be configured to use Java 8 features

Understanding Lambda Expression

Lambda expression looks a bit odd at first glance. The arrow operator (->), though it resembles the de-reference operator in C/C++, it has a different connotation here. This basically acts as a separator, which separates the parameters in the left side from the actual expression in the right side, for example -

(double val)->{return Math.sqrt(val);}

This is an anonymous function almost similar to the functions we write in Java. The function has a body, parameter list, and return value. What we are missing here is the return type definition and try/catch/throw expression if any, and a function name. These are all implicitly inferred by the compiler from the given expression. The bytecode after compilation has the expanded structure of the above code, which ultimately is consumed by the Java Virtual Machine. The instructions become terse and more readable (once you get accustomed to the Lambda structure of the code). This type of implicit instruction is the essence of functional programming as we have been speaking of earlier.

Lambda Coding

This is a very common code we write almost every day. There is nothing wrong in there; code is readable and clear to understand. It is our habit to iterate externally. We are almost programmed to think looping in such a way as we have learned imperative programming.

List<Character> vowels = Arrays.asList('a', 'e', 'i', 'o', 'u');
 
for (Character c : vowels) {
                System.out.println(c);
}

Observe the code below, there is no external iteration. The iteration is done implicitly. This, I believe, is more readable and terse than the previous code.

List<Character> vowels = Arrays.asList('a', 'e', 'i', 'o', 'u');                             
vowels.forEach((Character c)->System.out.println(c));

Now what about this...

List<Character> vowels = Arrays.asList('a', 'e', 'i', 'o', 'u');                             
vowels.forEach(c->System.out.println(c));

or this. The (::) is another interesting operator introduced in Java 8. We can reference the static function println with the help of this operator.

List<Character> vowels = Arrays.asList('a', 'e', 'i', 'o', 'u');                             
vowels.forEach(System.out::println);

The results are the same. Try running the above three examples.

Suppose there is a single function in the interface TestInterface as follows. We can call the function with a pair of empty parenthesis (as shown in Main).

public interface TestInterface {
                public void test() ;               
}
 
public class Main {             
                public static void main(String[] args) {                                           
                                TestInterface t=()->System.out.println("Hello");
                }
}

Conclusion

This is just a glimpse of what Lambda can do to your code when used appropriately. I think the basic take on introducing lambda expression in any Java code is that it can make your code terse, more readable. However, I presume this actually is the side effect of Lambda. The real reason to introduce Lambda in Java is to enable it for functional programming in multicore environments. I did not find any tested data to prove if it leverages performance. For now, just love the way it looks and program just for the fun of programming, for Eclipse is back on the track of Java 8. 


Tags: open source, Eclipse, Java 8, lambda expressions




Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel