The Most Useful and Interesting New Features in Java 8

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.

Lambda Expressions

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.

JavaScript Nashorn

Nashorn is the replacement introduced by Java 8 for the old Rhino JavaScript engine. Nashorn is an implementation of the JavaScript language written to run on the Java virtual machine or JVM.

In terms of speed execution, Nashorn can be compared to SpiderMonkey and V8. It also uses some features from Java 7, such as invokeDynamic for faster execution, and is considered a big improvement to older Java versions. Developers can now write JavaScript code directly into their Java code without execution speed, compatibility or interoperability issues.

Date/time APIs

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:

//Creating Objects.

LocalDateTime a1 = LocalDateTime.now(); // this will return current date and time.

LocalDate.parse(“02:53:15”); // Simple string input

Stream API

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.

Want to learn the ins and outs of Java 8?

Attend Codecamp in Iasi and join a two-day intensive training with Raoul-Gabriel Urma, the author of the bestselling programming book “Java 8 in Action.”

Leave a Reply

Your email address will not be published. Required fields are marked *