Java 8 is the first edition entirely made in Oracle for which there was a wait fed by doubts, fears and curiosities deriving from the acquisition of the Sun by Oracle. The attention of most of the international software community is now aimed at understanding what will be the effective support of Oracle to the Java world and its evolution, this major release will serve to outline the prospects.
This release promised new features and enhancements in different functional areas, increased developer productivity, significant performance gains through improved collections and annotations, simplified parallel programming models, and more efficient use of multi-core processors.
In the course of this discussion we will see what Java8 offers us again. The first observation we can make is that the number of Standard Edition packages has increased slightly, from 209 of the previous version to the current 217.
Lambda expressions and default methods
The Lambda expressions are a major innovation for the Java programming language. The focus in Java has always been on the data (classes, inheritance, generics, etc.), with this particular function the attention moves towards the behavior.
We have seen that if we want to model a flow like ” make A before starting, then make B for every file in this group, C if you find an error, and finally D when you are finished “, there are no suitable tools to express this behavior and this impacts on the API, for example by forcing the client code to be directly involved in every step.
Lambda expressions are the tool identified to overcome this problem. They are related to the concept of anonymous function , that is, a function that has a body but not a name. A lambda expression defines an anonymous function that maintains the state. In practice, a method without a declaration and therefore without a name, access modifiers, return type declaration.
Their syntax is something like this:
(arg1, arg2) -> {body}
Eg:
(int a, int b) -> {return a + b;}
Wanting to compare written code without and with lambda expressions, let’s look at the behavior of a button before and after.
//Before: button.addActionListener (new ActionListener () { @Override public void actionPerformed (ActionEvent e) { System.out.println ("Syntax arcinota!"); } }); //After: button.addActionListener ((e) -> { System.out.println ("New syntax!"); });
Here is another example where we use the lambda expression for simple video printing. The lambda expression appears twice, the former as done previously, while in the second we see that it is possible to convert a normal method into a lambda expression using the double colon ( ::) operator .
//Before: List list1 = Arrays.asList (1,2,3,5); for (Integer n: list1) { System.out.println (n); } //After: List list2 = Arrays.asList (1,2,3,5); list2.forEach (n -> System.out.println (n)); // Lambda expressions and double two points list2.forEach (System.out :: println);
Another new feature for the Java language is the default methods , new features that can be added to the libraries’ interfaces. It is now possible to add new methods to interfaces while ensuring compatibility of compiled with code written for older versions of interfaces (backward compatibility). As imaginable from the name, the classes that implement the interface but do not override the method in case of invocation of this method will offer the default implementation.
Nashorn and Javascript
Nashorn allows Java 8 to execute JavaScript on the JVM. Nashorn is an engine included in the JDK to allow any Java application to contain parts written in JavaScript. In addition, a command line tool ( jjs ) allows developers to use Nashorn as a scripting tool allowing the generation of self-supporting JavaScript applications. In practice, it takes the place of Rhino, JavaScript engine also Open Source, promising better performance and a greater integration between the Java world and the JavaScript world.
The idea is that JavaScript developers can use the wealth of libraries in Java and implement dynamic features by bypassing build cycles, while users can change configurations without regenerating the application.
Compact profiles
Have you always wanted to be able to instantiate a subset of the Java platform? The compact profiles allow this to be done, allowing applications that do not require the entire platform to be launched even on devices with reduced capacities.
This was achieved by identifying three subsets of the Java 8 APIs, offering the convergence between Java ME CDC ( Connected Device Configuration ) and Java 8. The smallest of these profiles (called compact1 , compact2 and compact3 ) requires around 11 MB, or less than four times the traditional JRE ( Java Runtime Environment ), evidently designed to work on platforms with pressing constraints in terms of available resources or performance / start-up times. In addition, deleting unused code can be useful from a security perspective perspective and to reduce application download times.
The Sun / Oracle side fear is based on the risk of excessive fragmentation of the Java SE, with the prospect of infinite variations and sub-variants that are practically unmanageable. But the advantages have prevailed over fears and this first subdivision of the libraries has been carried out, even if the complete implementation of the concept of modular platform is to be referred to Java 9. And of facts we speak of profiles and not of modules, where each profile it is a set that includes the previous one ( compact2 includes compact1 , and compact3 includes compact2 , thus including also compact1 ).
If we look at the libraries included in the various profiles, the basic outline we will find among others the inevitable java.io, java.lang, java.netand java.util, in addition to security libraries java.securityand javax.security. In the second profile there is evidence of the presence of java.rmi, java.sqland most of the libraries for XML. In the third profile appear the javax.management, javax.namingand javax.xml.crypto.
We have seen that the most compact profile is around 11 MB. The largest around 21 MB, well below the half of a classic Java SE 7.
In the next and last part of this discussion we will conclude our discussion on Java 8 by introducing some other relevant news of this update.
API for dates, time and streams
Java 8 introduces a new library to represent the time, java.timedesigned to provide support for the ISO 8601 international standard, introduce new features and overcome some problems encountered with the old APIs. The new classes have specific roles ( Clock, Year, YearMonth..) and types are immutable to simplify the management of competition in multitasking environments. This is obviously contrary to what happens with the classic Dateand Calendarbased on modifiable data that do not enjoy the thread safety and, above all, are not really intuitive (in java.util.Date, for example, the months start from 1 while the days from 0).
It was therefore decided to listen to requests for a new API able to remedy the problems of previous libraries, combining efforts with those of the author of a popular non-native library, Joda-Time .
To manage the concept of time difference , two classes are introduced, Durationand Periodwhich allow expressing and facilitating the calculation of time intervals. Periodit can be obtained as a difference between times ( .between(time1, time2)), while the Durationdifference between dates ( .between(date1, date2)).
Regarding the collections , Java SE 8 introduces the new package java.util.streams, designed to support operations involving flows of elements. The Stream API is integrated with the Collections API , allowing massive operations on data collections. It is not designed to supplant ArrayListsor other collections, but to manipulate data in a simpler and faster way, using an object to be used once and one way. Similar to the itator, with the stream we represent a sequence of objects. But, unlike the itator, in addition to sequential operations the stream supports parallel operations(allowing you to make the most of modern multicore architectures). Regarding the supported patterns, the streams are designed to perform filtering, mapping and reductions.
JavaFX 8
JavaFX , a set of libraries created for the development and distribution of modern Web applications on heterogeneous platforms, has become an integral part of the Java platform (hence the code version passed directly to eight). We will find a new default theme (“Modena”), a declarative language to create user interfaces starting from FXML (XML-based language to create user interfaces separating the application logic), a component able to integrate FX applications in the pages Web, API to manage printing and new APIs to manage three-dimensional graphics, support for HiDPI screens and enhanced support for text.
Through the use of lambda expressions the code management should be simplified. Furthermore, JavaFX 8 offers improved support for third-party components. Finally, significant progress is reported on performance.
Community involvement
The Java community has been essential to identify new features and requirements to be met, to discover bugs, to present useful solutions to improve Java 8. Through the JCP ( Java Community Process ) the propositive drive and the desire to contribute to the Java world are being organized. In particular, also through initiatives such as “Adopt a JSR program” ( Adopt-a-JSR program ), it is becoming easier and more transparent to contribute and be involved in the activities that orbit the Java world. Highlights are the JUGs ( Java User Groups ), essential nodes in the process of learning, participation and testing of the community.
Java ME 8
Along with a drastic increase in devices that need access to the internet, there is also an increase in the fragmentation of hardware architectures, software systems and support infrastructures, with the result of witnessing developments and standards entirely designed for a specific device. With Java 8 we try to take a step to contain fragmentation, as already seen with the compact profiles, moving towards the convergence between Java SE 8 and Java ME 8 .
Java Micro Edition 8 aims to provide a specialized, scalable and flexible development and execution environment for devices of all kinds. All this including standards, languages, security tools (and a community of over 9 million developers ready to offer their support), basing the entire stack on a layer (CLDC, Connected Limited Device Configuration , 8) designed to align Java SE 8 and Java ME 8 in terms of tools and programming model, as well as providing new features useful for specific environments.
CLDC 8 offers the recent constructs of the Java language, such as annotations and generics. Keeping in mind the subset of common constructs and APIs, it will be possible to create applications or libraries that can be executed without changes on a wide range of devices, from ultra-small devices to those created for the desktop or server world.
JVM and security
The focus in this new version of the JVM is focused on a more mature and efficient garbage collector , able to perform a greater workload concurrently. The basic design has not been changed but subject to incremental improvement, correcting the discrepancies identified in the flags related to the garbage collector .
Another aspect finally addressed, the management of the permanent generation area . This is an area used by the HotSpot VM to maintain metadata related to classes, data structures, etc., subject to being overloaded as the use of third-party libraries increases, with consequent OutOfMemory errors and requests to increase the MaxPermSize . The representation of the classes in the HotSpot VM has been re-engineered, removed from the permanent generation area and moved to the native memory areas or to the heap . Therefore, developers will no longer have to worry about establishing adequate dimensions for permanent generation .
As for security, there are several changes. Among them, stronger encryption algorithms, increased support for key management with a new option ( -importpassword) for command line instructions to safely import and store keys (as well as classes added or modified to improve their management), the implementation of a new variant of cryptographic hash function (SHA-224).
The main innovation concerns the default activation of TLS 1.2 ( Transport Layer Security ) on the client side. This is an encryption protocol that enables secure communication; it has been designed to encrypt communications, ensuring that no one can read or modify them, and can be combined with certificates to establish a level of confidence.
TLS was already present and active on the server side by default from JDK 7, but for reasons of backward compatibility it was not active by default client-side. TLS 1.2 has been set by default on JDK 8 also on the client side counting on greater maturity reached by the sector in terms of interoperability and backward compatibility and, above all, counting on the backward compatibility of TLS (version 1.2 is compatible with 1.1 and 1.0) and exploiting its transparency with respect to other applications. In this way the level of security of communications with respect to interceptions could be improved.