JDK 1.8 Features

JDK 1.8 Features


Following are JDK 1.8 Features

Read one-by-one as they are very innteresting.

1. Method References

a) With static method:


JDK 1.8 Features

Arrays.sort(names, Test::matchStringLength);

You can call the methods with their names (references) with :: operator.


2. Parallel Sort


JDK 1.8 Features

Both sort() and parallelSort() sorts the array. The performance with parallelSort() can be seen when the number of arrays to sort are very many. Same sorting can be done also using List or Set instead of arrays.

parallelSort() is overloaded as follows:

parallelSort(char[] a)
parallelSort(char[] a, int fromIndex, int toIndex)
parallelSort(byte[] a)
parallelSort(byte[] a, int fromIndex, int toIndex)
parallelSort(short[] a)
parallelSort(short[] a, int fromIndex, int toIndex)
parallelSort(int[] a)
parallelSort(int[] a, int fromIndex, int toIndex)
parallelSort(long[] a)
parallelSort(long[] a, int fromIndex, int toIndex)
parallelSort(float[] a)
parallelSort(float[] a, int fromIndex, int toIndex)
parallelSort(double[] a)
parallelSort(double[] a, int fromIndex, int toIndex)
parallelSort(T[] a)
parallelSort(T[] a, Comparator c)
parallelSort(T[] a, int fromIndex, int toIndex)
parallelSort(T[] a, int fromIndex, int toIndex, Comparator c)

3) Addition of Calendar.Builder

Before JDK 1.8, each date field is set separately with individual methods. Each set method added as a separate statement. See the calendar instance in the following code.


JDK 1.8 Features

In JDK 1.8, Calendar.Builder is used to instantiate calendar1 instance and all set methods are used as a single statement. Semicolon is given only one after build() method.

4) Introduction of Functional Interfaces

An interface containing only one abstract method is known as functional interface. For example, the java.lang.Runnable interface is a functional interface as it contains only one abstract method run().

A new annotation, @FunctionalInterface, is introduced to raise compilation error if an interface marked as @FunctionalInterface contains more than one abstract method.

The above interface compiles fine. The following does not compile, observe.

image
The Demo is marked as a functional interface but contains two abstract method. See the above screen for compilation error.

5) Lambda Introduction

The strength of JDK 1.8 is introduction of Lambda function.

A) Lambdas with forEach loop

JDK 1.8 introduces a new method forEach() to use with lambda expression.

JDK 1.8 Features

"-> " is known as lambda expression.

JDK 1.8 Features

Java 8 lambda expressions helps to write less code in a more readable way.

System.out.println(“\n\nTo print using Lamda in Java 8 in more readable way using type:”);
cities.forEach((String str) -> System.out.print(str + “\t”));

The lambda expression consists of two parts, left of lambda arrow and right of lambda arrow. Left gives the list of parameters and the right gives the body part. Here, the parameter is str and the body contains a single print() statement.

System.out.println(“\n\nTo print using Lamda in Java 8 without using type but using \”value\”:”);
cities.forEach(value -> System.out.print(value + “\t”));

The above forEach() can be written more concisely with method reference (using :: operator).

System.out.println(“\n\nTo print using more concisely with :: operator:”);
cities.forEach(System.out :: println);

6. Replacement of PermGen with Metaspace

JDK 1.8 officially announced the removal of Permanent Generation (PermGen) space and in its place introduced Metaspace. In HotSpot VM, the PermGen used to give OutOfMemoryError due to depletion of space, may sometimes be caused by memory leaks while loading and unloading a J2EE applications.

In practice, the IBM JRE and Oracle JRockit are not using PermGen space and are using the native memory (C-Heap) to store a class’s metadata. This concept is taken into JDK 1.8 for Java VM.

From JDK 1.8, most of the memory allocation for storing metada is done through native memory. The existing classes used to retrieve the metadata of a class no more works with metaspace. By default, the metaspace allocation memory depends on the native memory availability, OS virtual memory availability and on JVM of 32-bit or 64-bit. A flag introduced to limit the maximum memory allocation for metaspace – MaxMetaspaceSize. If this flag is not set, the metaspace will dynamically updated, at intervals, as per the requirement of the application running.

The garbage collector is triggered to go for garbage collection when the metadata usage is more than the size of MaxMetaspaceSize.

Due to removal of PermGen space, we cannot configure the space through XX:PermSize & -XX:MaxPermSize

8 thoughts on “JDK 1.8 Features

  1. arun

    sir ,
    your explanation superbbb and your way teaching is good .what are the features and explanation of that feature, so nice sir.
    Thank you

  2. Jyostna

    Hello Sir,

    In the second feature, your program prints both parallelSort() outputs? Is it correct? As you are telling the diff b/w sort and parallelSort methods.

Leave a Reply

Your email address will not be published. Required fields are marked *