Java gets going with version 8


Article from Issue 163/2014

In mid-March, Oracle released the eighth version of Java. In addition to small tweaks, the long-awaited release extends the core language, adding elements of functional programming – the first significant development since Java 5.

After two and a half years of work, long-serving Java development chief Mark Reinhold released version 8 of Java in March. Not only does it contain minor enhancements to the runtime library, it sees functional elements enter the Java universe in the form of lambdas.


Development of the new version was fraught with the same kind of issues as were experienced in the previous release: In August 2010, Reinhold pulled the ripcord and reduced the planned Java 7 feature scope so it could be completed in July 2011. The postponed features were due in version 8 at the end of 2012, which eventually became the beginning of 2014 – again with a reduced feature set.

The reason for the delay was mainly the much needed improvement of the security deficiencies in applets and Java Web Start [1]. The language and Java Runtime Environment (JRE) were actually planned from the beginning to prevent malicious code from breaking out of the designated sandbox, but the implementation of the concepts had significant weaknesses, so many Windows users made the unwelcome acquaintance of the BKA ransomware installed by a Java applet [2].

In particular, the planned modularization of JRE fell victim to the work of plugging security gaps (Project Jigsaw) [3]. Thanks to the ARM port, Java now runs smoothly on the Raspberry Pi, but you can hardly imagine an application on this little computer needing the entire Java standard library, including the Corba stack and LDAP support. With Jigsaw, the developers were also able to limit the runtime environment to the extent actually needed, thereby reducing the scope of the installation and the start time.


Programming languages are also fashion victims, and functional programming is currently gaining more and more followers. Unlike object-oriented languages, the focus here is on functions. Not only can they be defined and executed in the context of a class, but they represent a distinct entity. The programmer can pass them on and link them with other functions. Martin Odersky from ETH Lausanne has shown with his Scala [4] language what the combination of functional and object-oriented features in a single language can look like and do. In Java 8, these ideas are realized directly in the host language Java. In conjunction with its brief syntax, many tasks become much more compact than was previously possible in Java.

Java was designed from the ground up as an object-oriented language, so methods cannot simply be promoted to full-fledged language members like objects. In the background, therefore, minimal objects with only one method are used, and the process of writing them is expedited by the Lambda syntax.

The basis for implementation is the functional interface (FI). FunctionalInterface is an annotation for an interface with a method that represents, so to speak, the bridge between the object and the functional world. For example, Java 8 uses it in its well-known Comparator interface (Listing 1).

Listing 1

Java Comparator with FI


In previous versions of Java, at least one anonymous class is required to implement the interface (Listing 2, lines 4-9); in the worst case, the programmer defines the comparator in a separate class. This involves rather a lot of typing, whereas the actual computation in the example is no more than the code o1.width - o2.width. The Lambda notation in line 13 makes the whole thing much more compact. The following one-liner

ints.sort((Rectangle r1, Rectangle r2)-> r1.width - r2.width );

defines a comparator class with a sorting function and applies it to the list.

Listing 2

Lambdas in Java 8


A lambda function is defined in three parts: first the definition of the input parameters (Rectangle r1, Rectangle r2); then the so-called burger arrow (->), and finally the actual method implementation r1.width - r2.width. A method name is not necessary; after defining the Collection.sort method, the compiler already knows which FunctionalInterface fits the bill, and the FI in turn can contain only one method.

Java can also take the type definition for the input parameters from the interface so that the whole thing can be written in a more compact way, as shown in line 14. This reduces the original 155 characters to just 42.

Prepackaged Meal

As illustrated by java.util.Comparator (Listing 1), FunctionalInterface has been added throughout the standard library. In addition to the Lambdas, the syntax for Java 8 has been extended by two additional features: static methods defined in the interface and in the default implementation.

An example of the static method definition is shown in line 17 of Listing 2 The Lambda definition of the comparator from line 14 is nice and short, but you already know how to compare two numbers, so you don't need to reinvent the wheel to do that.

A ready-made implementation for this kind of trivial task can be defined as a static method in the interface in Java 8. The developer needs to tell the method how to access the numbers to be compared. In turn, this occurs as a Lambda function because, with the new syntax, you can also define method references.

The Rectangle::getWidth notation in Listing 2 refers to the getWidth method of the Rectangle class. The code applies this to any Rectangle objects passed into it, to compute the double values. The rules for sorting come from the comparator's static interface method, comparingDouble.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • JavaScript Alternatives

    JavaScript is the stuff of which many interactive web clients is made, but it comes with a fair amount of historical ballast. The creators of four alternative scripting languages seek to ditch the ballast.

  • Kotlin Programming Language

    Kotlin, a small island in the Gulf of Finland, is also the name of a new programming language that aims to become a modern alternative to Java.

  • CoffeeScript

    Through the years, many languages have tried to improve or even replace JavaScript, including CoffeeScript, which runs in all JavaScript Runtime Environments and compiles into JavaScript.

  • AJAX Workshop

    Books were the original model for website design. Navigation was similar to flipping the pages. Thanks to AJAX, many state-of-the-art websites now behave like desktop applications.

  • Google Web Toolkit

    The Ingenious Google Web Toolkit builds optimized JavaScript applications in a hurry.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More