Java SE 8 Beyond Lambdas: The Big Picture In addition to Lambdas, Java 8 features changes to the way annotations are handled, array processing, VM size, garbage collection, and so much more.



A lot has been written about JDK 8 and Lambdas, to the point where you might think nothing else really changed in this version of Java. In reality, so much has been added or improved, it's difficult to cover it all. In addition to Lambdas, which have been covered extensively, there have been changes to the way annotations are handled, array processing, VM size, garbage collection, and so much more.

I've already discussed parallel array processing and Spliterator s, so let's dive in and explore the rest.

Java Type Annotations

In the past, you could only apply annotations to declarations, such as with class and interface annotations:

@Entity public class Person { //... }

or on a method:

@Override public void run() { //... }

As of Java SE 8, annotations on Java types are now allowed. For instance, you can declare a String as requiring an entry:

@NonNull String name;

or object creation:

new @Immutable SomeObject();

or even Exception declarations:

void run() throws @Error SomeThreadException{ //... }

as opposed to something indicating a less severe situation:

void run() throws @Warning LatencyException { //... }

Default Methods

With previous versions of Java, if you added a method to an existing interface, all implementations of that interface would need to change. This naturally makes interface changes very disruptive. With Java SE 8, you can now add default method implementations to interfaces to avoid this situation. For instance, if you have an interface such as:

public interface MessageListener { public void onMessage(Message msg); }

You can now add a default method to avoid compiler errors in all implementing classes:

public interface MessageListener { public void onMessage(Message msg); public void onTextMessage(String msg) { TextMessage txt = new TextMessage(msg); onMessage(txt); } }

The Nashorn JavaScript Engine

Since Java SE 1.6, Java has shipped with a built-in JavaScript engine called "Rhino." Prior to Java SE 7, Rhino suffered the same problem that all JVM-based languages (besides Java) suffered: poor performance. However, with the addition of the invokedynamic bytecode in Java SE 7, all of these languages (Groovy, Ruby, PHP, JavaScript, and others) were treated as first-class languages and enjoyed the benefits of JVM runtime optimizations, such as JIT compilation and zero performance penalties when calling into Java code or libraries.

Taking this even further, Oracle built an entirely new, fully integrated, JavaScript engine called "Nashorn" with high performance as its primary goal. Thanks to its use of the Metaobject Protocol, JavaScript code in Nashorn can seamlessly call Java code (and vice versa) within the same JVM without any penalty. This allows you to consolidate all of your Java and JavaScript code onto one virtual machine, and leverage the same set of tools for Java development, debugging, profiling, and management.

Security Enhancements

Even with all of the added attention to security via the last set of Java SE 7 updates, Java SE 8 brings many of its own security enhancements to the JVM. This includes Base64 encoding via the new Base64.Encoder class, an implementation of the new Server Name Implementation (SNI) extension to the TLS/SSL protocol to indicate the hostname of the server a connection request is sent to, better checking of certificate revocation status, stronger algorithms for password-based encryption, and the addition of NSA Suite B cryptography with message digests. Oracle has published details for all of these security enhancements and more.

Library Enhancements

From the beginning of time (which circa 1995 for Java developers), working with date and time in Java has been troublesome. The APIs weren't thread safe, they were difficult to use and understand (that is, year starts at 1900, months start at 1, and days start at 0), and other basic concepts of time (such as time zones) are poorly handled or not supported.

With Java SE 8, Oracle has attempted to address all of this with new, developer-friendly APIs, which while easier to use, also manage to maintain backward compatibility with older versions of Java. Time-based standards are now supported including ISO-8601, BCP47, and CLDR for international date and time support; there are immutable implementations for improved thread safety and concurrency; and there's a domain-driven design to improve the APIs overall. For instance, standard getter -style APIs make date/time value retrieval much more straightforward:

LocalDateTime now = LocalDateTime.now(); LocalDate theDate = now.toLocalDate(); Month month = now.getMonth(); int day = now.getDayOfMonth(); int second = now.getSecond();