Thursday, 31 October 2013

What is new in Java 8 - Part 3


Lambda expressions are coming to Java in version 8. Lambda expressions are designed to allow code to be streamlined. When a Lambda expression is written, it is translated into a functional interface at compile time. Here is an example of using Lambda expressions to replace an anonymous inner class with much cleaner and more readable code.
  
Old way without Lambda:

button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println(“Action Detected”);
}
}
);
New way with Lambda:
button.addActionListener(e -> {
System.out.println(“Action Detected”);
}
);

No comments:

Post a Comment