Java SE 8 is knocking… Are You There?

We are hours  away from the official Java SE 8 launch …. March 18, 2014 being the GA (General Availability) date. Not that my opinion matters, but, I personally feel that all the Java technologies including Java SE 8, Java EE 7, Java FX 2.0 have been ‘publicized’ extremely well under Oracle’s stewardship. By ‘publicize’, I just do not mean ‘announcing’ stuff. Oracle has really done a tremendous job in sparking developer community interest in Java Platform (the fact that Java is so awesome makes it a little easier 🙂 But you still need to put in the hard work!)

So I guess it’s a good time to just glance through the ‘new’ stuff, hours before it hits the stands!

Although it’s tempting to dive deep into details of many of the features… I am going to abstain from doing that, stick to the agenda and leave the details for a later post 🙂

The features have been divided into logical categories

  • Core Java Library/API changes
  • Java Language changes
  • Java Platform changes
  • JVM changes

Core Java Library/API changes

The ‘domino’ effect of Lambda Expressions

All the core libraries/classes have been completely re-factored to replaceanonymous inner classes‘ with equivalent lambda expressions.

Note: These changes are not just ‘syntactical’ in nature. The new byte code ‘invokedynamic‘ (introduced in Java SE 7) has been leveraged behind the scenes for producing performance improvements out of lambda expression usage

The Date and Time API (JSR 310)

Finally! The much awaited, revamped, completely rewritten Date and Time API with Thread Safe and Immutable implementations is here! No more dependency on java.util.Date or java.util.Calendar.

Collections Framework: Bulk Data Operations

java.util.stream package unleashes a new interface – Stream. It’s like a java.util.Iterator on steroids! Obtain a Stream from a Collection, pass in the parameters in the form of Lambda expressions. One can also leverage the interfaces from the java.util.function (Function, Predicate, Consumer, Supplier) package in order to perform operations on the Stream of items.

e.g. Walk through list of users, filter them as per department, and log their names

users.stream()
.filter(u -> u.getDept() == Department.INFOSEC)
.forEach(u -> log(u.getName()));

Note: In case you have a sufficiently large data set and intend to leverage modern multi-core CPUs, a parallel version of the Stream can also be obtained via the parallelStream() method in java.util.Collection. The ForkJoin framework handles the parallel processing behind the scenes.

users.parallelStream()
.filter(u -> u.getDept() == Department.INFOSEC)
.forEach(u -> log(u.getName()));

Parallel Array Sorting

parallelSort() method and its overloaded versions have been introduced to the java.util.Arrays class. Again, the ForkJoin framework works behind the scenes to split the sorting task into sub tasks and executing them in ‘parallel’ before ‘merging’ the results.

The ‘not so huge’ library changes

  • ForkJoin Pool which was first introduced in Java SE 7 has been further enhanced
  • JDBC 4.2 has ushered in improvements w.r.t all our favorite interfaces – java.sql.ResultSet, java.sql.PreparedStatement, java.sql.CallableStatement. These have been changed to factor in the ‘new’ types – mainly the Date and Time classes
  • Finally, we have a standard, public API for encoding/decoding! The public equivalents of the sun.misc.BASE64Encoder and sun.misc.BASE64Decoder have been introduced. These are the java.util.Base64.Encoder and java.util.Base64.Decoder respectively


Java Language changes

Lambda Expressions (JSR 335)

  • Responsible for introducing the ‘Functional‘ programming paradigm to Java.
  • From a syntactical perspective, Lambda expressions look to eradicate code verbosity resulting due to usage of anonymous inner classes resulting from implementation of interfaces (which mostly have a single ‘abstract’ method).Think about how you generally use java.util.Comparator or java.lnag.Runnable interfaces. Going forward, such interfaces are going to be referred to as a ‘Functional Interface
  • Under the covers, at run time, invokedynamic is being leveraged in order to provide performance boost.

Type Annotations (JSR 308)

  • Currently, Annotations can only be applied to type declarations such as classes, methods and field variables.
  • From Java SE 8 on wards, we can use annotations in any place where a Java type might be used e.g. method parameters
  • In the below example, @NotNull is a custom Type Annotation which can be included into the Java type system in form of ‘plugins’ and can help detect potential causes of NullPointers at compile time.

public void aMethod(@NotNull String aParam)

Extension Methods

  • It’s not possible to add methods to an existing interface without breaking the classes which implement it.
  • Java SE 8 has bought in the concept of ‘Default’ methods which can be added to existing interfaces. These methods can contain implementation logic.
  • This means that an additional method can be added to an existing interface and it has to be specified using the ‘default‘ keyword. The existing classes which implement the interface will continue to compile without any issues

Access to Parameter ‘names’ at run time

The java.lang.reflect.Method class now inherits from a ‘new’ class – java.lang.reflect.Executable which has a getParameters() method returning an array of java.lang.reflect.Parameter objects which can be further introspected to find out the name. This is particularly useful for code generation tools and IDE developers in general

Java Platform changes

Compact Profiles

  • Prepping for ‘Project Jigsaw‘ (due in Java SE 9) which aims at ‘modularization’ of the Java SE platform
  • Support for ability to include only the ‘required’ components for an application rather than the ‘complete’ run time (140 MB).
  • Ability to deploy application on devices with less computing resources
  • Better performance
  • Introduction of 3 different profiles, namely, Compact1Compact2 and Compact3 with memory footprint of 10 MB, 17 MB and 24 MB respectively.

Dependency Analysis Tool

  • A new command-line tool jdeps, has been added so that developers can use it to understand the static dependencies of their applications and libraries.
  • This can also help developers better figure out as to which ‘compact’ profile might be applicable for their implementation.
  • It also provides an –jdkinternals option to find dependencies to any JDK internal APIs that are unsupported and private to JDK implementation

Stripped Implementations

Ability to create applications or executables which contain the application code, and a custom JRE (limited libraries + JVM)

Restrictions: Only the ‘particular’ application can run with this configuration. This ‘restricted’ run time cannot be used to run other applications – this has been put in place in order to prevent violation of the ‘Write Once, Run Anywhere’ paradigm.

Note: This feature has been left out of the Java SE 8 release. Still wanted to drop in  a line about this

JVM related changes

invokedynamic‘ implementation itself has been has improved (via the JSR 292 RI). The improved version is being leveraged for executing Lambda Expressions at run time in order to achieve maximum performance benefit

Oracle Nashorn –  Brand new high performance JavaScript engine. Will replace Mozilla Rhino (shipped with JDK 7)

Permanent Generation – its no more! It is being ‘permanently’ removed from the HotSpot JVM and is being replaced by ‘Metaspace‘ (native memory region). It can be controlled via the following flags – MetaspaceSize and MaxMetaspaceSize. This obviously means no more “java.lang.OutOfMemoryError: PermGen space” which is obvious because there is no PermGen

I will continue to blog about the new Java SE 8 features in detail and provide code driven insight/examples in my upcoming posts.

Until then, Stay Hungry, Stay Foolish!

Cheers! 🙂

About Abhishek

Loves Go, NoSQL DBs and messaging systems
This entry was posted in Java, Java SE and tagged , , , , . Bookmark the permalink.