Love and hate for Java 8

Java 8 brings exciting developments, but as with any new technology, you can count on the good, the bad, and the headaches

RELATED TOPICS
Page 2 of 5

The big benefit of this feature is the ability to use multiple processor cores for collection processing. Instead of doing the traditional for loop, use Stream in parallel mode -- theoretically, the speed goes up with each core added. The main problem that could arise from this is readability. With all of the chaining of streams, the lines could get long, which will affect readability. The other problems stem from the things that are built to support this new path. Those are functional Interfaces and Lambda.

Functional interfaces

Java 8 will have a new feature called functional interfaces. Basically, default methods are added to an interface and do not have to be overridden in the interface implementation. These methods can be run directly from the interface.

This was done for backward compatibility for your collections in your interfaces. It is to solve the problem of allowing the Stream to be put into an interface without having to change all of the classes to implement the new method. Basically, create a default method in the interface, and all the classes that implement the interface can use the Stream (or whatever is put into the default method). If the default method is not correct for the implementation, it can be overridden in the implementer.

What this essentially does is allow a form of multiple inheritance. This becomes the implementer's problem, as the implementer will be required to override the method anyway. The implementer could then choose which supermethod to use, but this means a lot of classes that implement interfaces could still be changed.

This is probably the detail that will concern most people in Java 8. Perhaps it won't bother those already familiar with Scala. It can be compared directly with the concept of traits in Scala; although the concept is not unique to Scala, it is best known to the Java world from Scala. However, there are some differences: Java 8 functional interfaces can not get a reference to the implementing class. Scala allows this with the self keyword. Language nerds will say that Java 8's functional interfaces allow multiple inheritance of behavior, but not state. Scala's traits are multiple inheritance for both behavior and state.

Consider the power of traits and the stuff we do to work around not having them in Java. In Java to implement transactions and other items, we construct dynamic proxies and do bytecode manipulation with JavaAssist or extend classes with cglib. Traits give us ways to do this more directly in some cases.

On one hand, functional interfaces will probably be misused the way that inheritance is misused. On the other hand, they don't go as far as Scala's traits. We'll still be stuck with some level of bit-twiddling class-loader implementation via Aspect Oriented Programming-type notes in Java in places where traits could probably stand in.

Lambda
Lambda expressions are coming to Java in version 8. Lambda expressions are designed to allow code to be streamlined. When a Lambda expression is written, it is translated into a functional interface at compile time. Here is an example of using Lambda expressions to replace an anonymous inner class with much cleaner and more readable code.

Old way without Lambda:

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ae) {

System.out.println(“Action Detected”);

}

}

);

New way with Lambda:

button.addActionListener(e -> {

System.out.println(“Action Detected”);

}

);

RELATED TOPICS
| 1 2 3 4 5 Page 2
From CIO: 8 Free Online Courses to Grow Your Tech Skills
View Comments
Join the discussion
Be the first to comment on this article. Our Commenting Policies