Java 8 Features Simplified

Hello and welcome , This article will walk you through some of the most significant java 8 features one by one. You will learn how to implement default interface methods, lambda expressions, method references, and repeatable annotations with the help of short and easy code samples. You’ll be familiar with the most significant API updates, such as Interface in java 8, Lambda Expressions, Functional Interface, Stream API and the new Date API, by the end of the article.

Interface in java 8:

Prior to Java 8, methods in Interfaces that we declare are by default public abstract even if we don’t specify and we can only declare abstract methods that cannot define the implementation. Interfaces in Java 8 can have default and static methods. The default methods in interfaces are there to allow developers to add new methods to the interfaces without impacting the classes that implement them.

For example, If various classes, such as A, B, C implements the Interface Z, adding a new method to the Interface Z requires changing the code in all of the classes that implement the interface. In this case A, B, C.

As a result, we have a new concept called “default methods” in Java 8. These methods can be added to any existing interface, and they are not required to be implemented in the implementation classes, so we can add them to existing interfaces without affecting the code. To achieve this we need to use the default keyword to the method

.

Static methods

Lambda Expressions:

//Syntax of lambda expression

(parameter_list) -> {function_body}

To create a lambda expression, place the input parameters (if any) on the left side of the lambda operator -> and the expression or block of statements on the right side.

Things to keep in mind when creating a lambda expression

  • No modifiers
  • No return type
  • No method name

Let’s look at some examples on how to convert classic methods to Lambda Expression!

Converting a method with no arguments

Converting a method with arguments and return type

Functional Interface

What happens when you mark an interface with @FunctionalInterface annotation? Compiler will recognize its a functional interface and will give an error when you add a second method. This safely avoids adding methods in the future.

“What are the rules in functional interface?”

Only one abstract method should be in the functional interface. They can have any number of default and static methods in addition to the abstract method.

Example of Custom functional interface

Stream API

“Let’s look at some example with and without streams to easily understand the concept”

Finding a string with certain conditions in the list without using streams

Finding a string with certain conditions in the list using streams

“But why use Streams?”

Streams use something called Parallel Execution of Operations, which greatly enhances the code’s performance.

In the first example, we’re iterating through the entire list to find strings that are less than four characters long. This code contains no parallelism.

In the second example, the stream() method returns a stream of all the cars, the filter() function provides another stream of cars with a length less than 4, and the count() method reduces this stream to the result.

All of these operations are carried out in parallel, meaning that we can use streams to parallelize the code. Using streams to parallelize operations is faster than doing it sequentially iterating through the list without using streams. This is very highly useful when there are thousands of records in the collection, streams will produce the result in a shorter amount of time, improving performance.

Java 8 Date/Time:

LocalDate- Only the Date is represented by the LocalDate class. There isn’t any information about the time or the time zone.

Implementation and output

LocalTime- Only the Time is represented by the LocalTime class. There isn’t any information about the time or the time zone.

Implementation and output

LocalDateTime- Both date and time is represented by the LocalDateTime class. There isn’t any information about the time or the time zone.

Implementation and output

Instant and Duration- The instant class is used to represent a time stamp. On Instant, you can perform plus, minus, and a variety of other operations and on the other hand Duration class represents time interval between two instants in second or milliseconds

Implementation and output

Conclusion

Since the release of Java 8, we’ve been using some of the most important features. These features have significantly enhanced performance, simplified development activities, and increased Java’s efficiency. This article summarizes and explains with very simple examples on some of the most significant features released in java 8 that every developer must know. I hope that you find this article to be useful and that you liked reading it.