Tips & Tricks

6 Major Enhancements in Java 8, Programmers Should Be Aware Of

If you’re a Java programmer, then you’ll most likely be familiar with Oracle JDK (Java Development Kit) 8. Java 8 has been one of the most anticipated version of Java that introduces lots of new features, improvements and bug fixes, thereby enhancing the efficiency of Java professionals in developing and running programs.

In this post, we’ll explore some of the major enhancements that have been included in Java 8:

Lambda Expressions

The main highlight of Java version 8 is that it supports Lambda expressions. Although, the Lambdas have been a part of JVM since its inception, but developers had no choice till now to avoid using Lambdas with standard anonymous classes. But finally, the new Java version has enhanced the way how Lambda expressions are implemented. The Java 8 Lambda expressions substitutes an anonymous inner class with more clean and easier-to-read code. Let’s look at an example that verifies the same:

[gads]

Code without Lambda:

[cc lang=”java”]
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println(“Action Detected”);
}
});
[/cc]

Code with Lambda:

[cc lang=”java”]
button.addActionListener(e -> {
System.out.println(“Action Detected”);
});
[/cc]

As you can see in the above code, the new enhanced Lambda expressions features helps in reducing the lines of codes. And thus, makes the code streamlined.

Improvements to Functional Interfaces

The new functional interface feature of Java 8 will allow programmers to define static methods. For instance, a static method named naturalOrder method has been added to java.util.Comparator. Below is an example that demonstrate the use of this new static method:

[cc lang=”java”]
public static >
Comparator naturalOrder() {
return (Comparator)
Comparators.NaturalOrderComparator.INSTANCE;
}
[/cc]

What’s more? The new feature will also allow interfaces to define default methods. For this, a forEach method was embedded in the java.lang.Iterable library. Let’s have an overview of an example that make use of the forEach method:

[gads]

[cc lang=”java”]
public default void forEach(Consumer action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
[/cc]

In a nutshell, earlier Java libraries couldn’t add methods (static methods) to the interfaces. That’s because, adding a method to the interfaces resulted in breaking the entire code implementing the interface. But with the new functional interface, Java libraries can now add methods to the interfaces. Besides, in the new Java version several default methods are added to the core JDK interfaces.

Faster Sorting

The way concurrent adders accelerate counting, likewise Java 8 offers a succinct way to expedite parallel sorting. The trick is that rather than using [code]Array.sort(myArray)[/code], Java 8 let you use [code]Arrays.parallelSort(myArray)[/code]. This helps to break the array into several pieces, which are sorted independently and then recombined.

However, there is a commonition with this process when used in highly multi-threaded environments. For instance, when called in a “busy web container”, the benefits of sorting the array independently to speed up the process will begin to recede, because of the increase in the cost of CPU context switches.

Improved New Date/Time APIs

For over the past several years, Java developers have been struggling to cope up with the complexity of manipulating date and time using the standard date and time classes. With Java 8, a new take to improve date and time manipulation has been made, by providing “Joda-Time” a viable alternative for the complex date/time Java API.

Joda-Time helps to tackle the problems that developers have encountered while using the standard date and time classes; and has become the most sought-after date and time library. Unlike the [code]Calendar.getInstance()[/code], Joda-Time allows to use multiple calendar systems, and yet provides a simple API. The Joda-Time library is easy-to-use and comes loaded with the functionality needed for performing for date-time calculations. There’s lot more that will make you fall in love with the new date/time APIs for Java.

Enhanced Security

Many Java users faced some serious security flaws, with Java 8, Oracle believes that most of the security issues will be fixed at a fast pace. And will eventually improve the Java security model, introducing some new security features. In order to improve the security, the current manually maintained list of caller-sensitive methods will be substituted with a mechanism. The mechanism will help determine caller-sensitive methods accurately, allowing the callers to be found in a reliable manner.

For example, Java users have discovered a bug when implementing [code]String.hashCode()[/code] in Java. According to this known bug, when a set of HashMap are introduced, it might result in a DOS (denial-of-service) attack. In essence, in case hash is added to the same value by many parameters, then it results in exceeding the CPU time.

At present, the HashMap algorithm use linked list so as to store the map entries. In case the algorithm causes a number of collisions, then complexity of HashMap will be O(n) instead of O(1). To fix this issue the bucket is switched to using the balanced tree algorithm – when the bucket items reaches a certain threshold. This will help lessen the complexity to O(log n), and deal with denial-of-service bugs.

Introducing Concurrent Accumulators

Concurrent programming generally involves updating numeric counters that are accessed by multiple threads. Over the years, this process has been performed using “synchronized blocks”, to “read/write locks” and “Atomic Integer(s)”. Although, the latter ones have proved to be efficient, considering that they depend directly on processor CAS instructions. However, they demand having good familiarity with how you can implement the semantics in the correct manner.

Luckily, this problem has been solved with the introduction of Java 8 at the framework level, by making use of new concurrent accumulator classes. These classes help to increase or decrease the value of variable counter efficiently in a thread safe manner.

Let’s Sum Up!

If you’re a Java programmer, and have encountered several issues while working in a Java based applications, then the new Java version 8 will bring forth a rich set of enhancements and improvements for you. Wondering what you can expect with the Java 8? Hopefully, reading this post will give you a basic understanding of some of the major improvements you should look forward to in Java 8.

Shares:

Leave a Reply

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