Java gets going with version 8
Refurbishment
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.
Postponed
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.
Lambdas
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
(incl. VAT)
Buy Linux Magazine
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.
News
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.