Top 6 Java 8 features you can start using now

Unlike Java 7, Java 8 has some significant changes. You can get familiarised with the the following simple working code. All you need is Java 8 installed on your machine.

#1: Interface can have static and default methods. This tries to solve the diamond (aka multiple inheriance) issue. In the past it was essentially impossible for Java libraries to add methods to interfaces. Adding a method to an interface would mean breaking all existing code that implements the interface. Now, as long as a sensible default implementation of a method can be provided, library maintainers can add methods to these interfaces. The static methods serve as utility methods, hence you don’t need a separate helper class.

#2: Lambda expression for supporting closures, which simplifies your code. In the past, you need to write anonymous classes. Lambda expressions are anonymous methods which are intended to replace the bulkiness of anonymous inner classes with a much more compact mechanism.

Here is a basic example that covers #1 and #2. A simple button click example.

Let’s define an interface with a static method (e.g. a utility function) and two default methods as shown below.

Define the TestImpl class, which implements the interface Test. The TestImpl uses closures via lambda expressions and makes use of the methods implemented in the interface Test.

If you click on all 4 buttons from B1 to B4, you will get an output of

Q. How does ” button3.addActionListener((e) -> {System.out.println(“B3 clicked …”); testObj.doWork(); }); ” know what method to execute? Did you notice that there is no new ActionListener( ) and no public void actionPerformed(ActionEvent event)?

A. Here the compiler understands that there is only one method in the ActionListener interface and that takes only one parameter. So obviously, it decides (finalizes) that the e as a reference to ActionEvent.

The functional interfaces that are annotated with @FunctionalInterface in Java 8 ensures that you can only define one abstract method. You can define as many default and static method implementations as you want.

#3 Java 8 adopts Joda as its native date & time framework. Due to what was perceived as a design flaw in Joda, Java 8 implemented its own new date / time API from scratch.  The new APIs were designed with simplicity in mind.

All the core classes in the new API are constructed by fluent factory methods. When constructing a value by its constituent fields, the factory is called of; when converting from another type, the factory is called from.


The output will be:


Example 1: The java.lang.Runnable interface has been made a functional interface.


Output:

main thread: main
worker thread: Thread-0
worker thread: Thread-1

Example 2: The java.util.function package has a number of functional interfaces.

Output:

consumed: 5
consumed: 25
consumed: 6
consumed: 36

Example 3: The Function is similar to Consumer, but the Function interface has a return value in its apply(T t) method, whereas the Consumer  has a void method accept(T t).

Output:

result1= 10.0
result2= 25.0
result3= 144.0

Example 4: The Predicate interface from java.util.function package has test(T t) method that returns a boolean value indicating test condition has passed or failed. The predicates are often used to  filter a collection of objects.

Output:

short names: C++
short names: PHP
short names: C#
starts with J: Java
starts with J: JRuby
starts with J: Jython


800+ Java Interview Q&As Menu

Learn by categories on the go...
Learn by categories such as FAQs – Core Java, Key Area – Low Latency, Core Java – Java 8, JEE – Microservices, Big Data – NoSQL, Architecture – Distributed, Big Data – Spark, etc. Some posts belong to multiple categories.
Top