JDK 8 - 6 features that actually matters most to Java developers!

Type “new updates in Java 8” across search engines and you will immerse yourself into a plethora of slide shares, articles, blogposts, etc. focusing of number of new features introduced in JDK 8. Of these, and no offence to anyone, I found 6 features that actually matters to Java developers’ community. 

In this post, I attempt to capture these new features with code snippets wherever applicable.

JDK 8 released in the spring of 2014 has new features which are useful in many aspects like reducing coding, improving performance and security. I would like to share some of the most prominent features of JDK 8.

JDK 8 comes with features like,

  • Lambda expressions and streams which simplifies coding and helps developers code easily
  • Time package avoids the need to use third party APIs and helps to better handle date and time
  • Performance improvement via parallel sorting and parallel streams
  • Nashorn JavaScript engine is a light weight and high performance JavaScript engine and
  • Improved security

1. Lambda expressions -

This is a major feature in JDK 8, which helps developers pass the functionality as method argument or pass the code as data. The code can be written easily and meaningfully using Lambda expressions and streams. Lambda expression provides the simplest way to implement the interfaces with single method which will be more convenient to developers. For example, EventHandler interface has only one method called handle. Prior to JDK 8, we need to write the code as below to override the handle method.

btn.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
System.out.println("Hello World!");

The same can be written as below using lambda expression

event -> System.out.println("Hello World!")

2. util.stream -

This is a new package added in this release. This API is integrated into the Collections API. Using streams the collections can be processed in sequential or parallel. Writing more than 5 lines of code to iterate a collection can be written in single line with stream and lambda expressions. For example, See the below code to sum the weight of all the widget having the color red in a collection of widget. Widgets is a collection having n number objects of Class Widget. Here stream and lambda expression made the work in single line instead of 5 lines of code.

int sum = widgets.stream().filter(w -> w.getColor() == RED)

.mapToInt(w -> w.getWeight()).sum();

3. Compact profiles -

Today to develop java application we use java SE API which has all the packages in it. We may use only limited set of packages from the complete list of packages. JDK 8 brings the future called Compact profiles. A Compact profile is a profile with limited set of packages which can be used for application development. There are three types of profiles. They are Compact profile 1, Compact profile 2 and Compact profile 3. Compact profile 1 has the following set of packages like Core, Security, Serialization, Networking, Ref Objects, Regular expressions, Date and time, Collections, Logging, Concurrency, Reflection, JNDI, JAR, ZIP, Versioning, Internationalization and Scripting. Profile 2 has all the packages in profile 1 plus the following packages like JDBC, RMI and XML JAXP. Profile 3 has all the packages in profile 1 and profile 2 plus the following packages like Security, JMX, Management and instrumentation. Finally Full SE API has all the packages.

Depends on the requirement the application can use any one of the profile which will reduce the JRE memory and this feature will be useful for applications development for small device applications which has memory constraints.

4. Date-Time packages - 

Java.time is a new package added in jdk1.8 which comes with new api to resolve the drawbacks in the previous versions like no thread safe, poor design and difficult to handle time zone. All the classes in this new package are immutable and thread safe. This new package provides lot of date time related features, so there is no need to use any third party date time API in most of the cases. For example: Let’s take a simple scenario, you have an application which needs to force the user to change the password every 60 days. So we need to find the number of days between password last changed date and the login date.

Prior to JDK 8, this was achieved using third party api’s like below.

Using joda-time:

int passwordExpireDays = DAYS.daysBetween(passwordLastChangedDate, new Date()).getDays();

Using Jdk 8:

long passwordExpireDays = ChronoUnit.DAYS.between(passwordLastChangedDate, new Date());

5. Parallelism -

Parallelism means dividing the problem into sub problems and solving those problems simultaneously. Parallelism is achieved through Fork/join framework which was added in JDK 7. In JDK 8, parallelism is available in two places. One is Arrays sorting in parallel and second in executing streams in parallel. The Arrays class is enhanced with additional utility methods to perform parallel sorting on primitives and comparable objects.

For example,

  1. Parallel sorting can be achieved through parallelSort() method in Arrays.
  2. The collection classes are enhanced to provide the parallelstream like below.
int sum = widgets.parallelStream().filter(w -> w.getColor() == RED).mapToInt(w -> w.getWeight()).sum();

6. Nashorn JavaScript engine -

Nashorn is an implementation of JavaScript language written to run on java virtual machine. Prior to this, Java SE had Rhino JavaScript engine. Nashorn provides better runtime performance than Rhino. It provides integration between Java and JavaScript languages. Nashorn can be used either in Java programs or by using jjs command line tool available inside JAVA_HOME/bin directory.

For example:

Accessing JavaScript from Java programs:

ScriptEngine myEngine = new ScriptEngineManager().getEngineByName(nashorn);

myEngine.eval(print(Hello Welcome!););

Using jjs command line tool:

jjs.exe cannot be accessed directly, so we need to create a symbolic link. Open command prompt and create the link like below.

C:\Windows\system32>mklink jjs.exe C:\Program Files\Java\jdk1.8.0_05\bin\jjs.exe

Now the link is created so we can access jjs tool and run the JavaScript code like below.


jjs> print('Hello Welcome');

Hello Welcome

Accessing JavaScript files from Java:

ScriptEngine myEngine = new ScriptEngineManager().getEngineByName(nashorn);

myEngine.eval(new FileReader(sample.js));

Accessing Java from JavaScript:

Create a JavaScript file called sample.js with the below code

var ArrayList = Java.type(java.util.ArrayList);

var list = new ArrayList();




for each (var el in list) print(el);

for each (var el in list) print(el);

Open the command prompt and run the sample.js using jjs command like below.

C:\Windows\system32>jjs sample.js





Overall JDK 8 provides lot of features which most of the developers love and like to use. JDK 8 features helps reduce coding time and makes the code more readable, improves performance in specific places like sorting and iterating collections using streams, reduces the usage of third party tools for achieving parallelism and date time operations, and more.