According to the Tiobe index for February 2017, Java is one of the most searched programming languages, ranking number one in the index.
When Java 8 was released three years ago, the IT community saw it as a huge improvement to Java, tackling every aspect of the programming language, including JVM (Java Virtual Machine), the compiler, and other help-system enhancements.
Nowadays, Java 8 is famous among developers for many features. The most useful and interesting ones can be found below.
Although lambda expressions are popular in functional programming, Java didn’t support them before 2014 and was thus missing one of the most efficient ways to write code. Java 8 introduced lambda expressions to provide a simple syntax to create and use lambdas.
Prior to lambda expressions, developers had to use anonymous boilerplate classes with lambdas which was a tedious process. With lambda expressions, functions can be used anywhere and can be treated as code.
In order to use lambda expressions, you need to use a comma-separated list of parameters or collection, and the symbol “->” within the body. In the example below you can see the syntax of the lambda expression:
Arrays.asList(“k”,”l”,”m”).forEach( m -> System.out.println(m));
Java 8 also introduced the concept of functional interface to allow functions to take lambdas as arguments. In Java 8, lambda expressions are treated as a special implementation of a functional interface, meaning that, in order to receive a lambda as a method argument, that argument’s declared type only needs to be a functional interface.
Another great addition to Java 8 is the complete date/time API. Before it, developers would use Joda, the go-to Java date time API, which wasn’t very good but was used by Java developers to compensate for the lack of support.
Given the complexity of the Java API library, Java 8 rewrites the whole API from scratch, adding a new method to the Date class called toInstant() which converts it to the new API. This enables developers to make a quick jump to the new API even if you’re working with code that uses the old Date API.
Here’s an example of the new API:
LocalDateTime a1 = LocalDateTime.now(); // this will return current date and time.
LocalDate.parse(“02:53:15”); // Simple string input
Thanks to the lambda syntax, Java 8 also included the Stream API, which enables developers to work with Collections. The Stream API helps developers look at Java collections from a new angle. The complete documentation on the Stream API can be found here.
With the help of lambda functions and the Stream API, developers write more meaningful, simpler code.
Some believe that Java 8 is an epochal release of the Java platform. The introduction of lambdas has brought more functional programming abilities into Java, while other features are perfect for working with asynchronous programming. Everything marks a big step forward for the Java language, enhancing Java development and making it more efficient and more interesting.