Search This Blog

Friday, 20 January 2017

What's New in Java 8

Java Platform, Standard Edition 8 is a major feature release. Java 8 contains a large number of new features. Although all are important, the three most important one are:
  • Lambda expressions
  • The stream API in java.util.stream
  • Default interface methods

Lambda Expressions
The most important new Java 8 feature is the lambda expression.
Lambda expressions are very important as they add functional programming features to Java. Lambda expressions can simplify and reduce the amount of source code needed to create certain constructs, such as some types of anonymous classes. This is particularly helpful when implementing a number of commonly used event handlers.
To support lambda expressions Java has been expanded by the inclusion of a new operator (the –>) and a new syntax element.


To understand the benefits that lambda expressions bring, consider the following ActionEvent handler, which uses the traditional, anonymous class, approach:

myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
myLabel.setText("Button pressed.");
}
});

With JDK 8, this event handler can be written using a lambda expression

myButton.addActionListener(
(ae) -> myLabel.setText("Button pressed.")
);


As we observe code is shorter, that is more direct and to the point.



The Stream API
Another important feature of Java 8 is the new stream API, which is packaged in java.util.stream.
A stream represents a sequence of data. The key aspect of the stream API is its ability to perform pipeline operations that search, filter, map, or otherwise manipulate data.

Let’s assume we have a list that stores employee names, department, e-mail and phone numbers. Using the stream API, we can efficiently pipeline the operations that search for entries that match some criterion, for example department name, sort the matching items, and then extract only the e-mail addresses In many cases, such actions can be performed in parallel, thus providing a high level of efficiency. The stream API provides a powerful means of handling data in an efficient, yet easy to use way.

Default Methods
With the release of Java 8, it is now possible for an interface method to define a default implementation. This new capability is called the default method.

Default method enables us a means by which interfaces could be expanded without breaking pre-existing code.
In simple terms default methods enable us to add new functionalities to interfaces without breaking the classes that implements that interface.


When a non-abstract class implements an interface, it must implement all methods defined by that interface. If a new method is to an existing interface, then the addition of that method would break pre-existing code, because no implementation would be found for that new method in pre-existing classes. The default method solves this problem by supplying an implementation that will be used if no other implementation is explicitly provided. Thus, the addition of a default method will not cause pre-existing code to break. This enables interfaces to be gracefully evolved over time without negative consequences.


1 comment:

  1. Nice article!. Can you please post about java9 also?

    ReplyDelete