Channels ▼

Eric Bruno

Dr. Dobb's Bloggers

Java SE 8 Beyond Lambdas: The Big Picture

March 31, 2014

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 Spliterators, 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();

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video