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

  • Celin
  • 0
  • Jan 06, 2015
  • Tips & Tricks

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:

Code without Lambda:
button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { System.out.println(“Action Detected”); } });
button.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent ae) {
    System.out.println(Action Detected”);
  }
});
Code with Lambda:
button.addActionListener(e -> { System.out.println(“Action Detected”); });
button.addActionListener(e -> {
  System.out.println(Action Detected”);
});

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:

public static > Comparator naturalOrder() { return (Comparator) Comparators.NaturalOrderComparator.INSTANCE; }
public static <T extends Comparable<? super T>>
 Comparator<T> naturalOrder() {
 return (Comparator<T>)
 Comparators.NaturalOrderComparator.INSTANCE;
}

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:

public default void forEach(Consumer action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } }
public default void forEach(Consumer<? super T> action) {
  Objects.requireNonNull(action);
  for (T t : this) {
    action.accept(t);
  }
}

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 Array.sort(myArray), Java 8 let you use Arrays.parallelSort(myArray). 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 Calendar.getInstance(), 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 String.hashCode() 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.

Related Posts

Written by Celin

Celin Smith is renowned web developer and blogger at Xicom who loves to write regarding mobile and web applications. Xicom is one of the amongst leading Java Web Development Outsourcing Company which provides totally different array of real software package solutions like Custom software Development IT Outsourcing Services, Web Application Development services to its clients globally.

View all posts by: