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:

import java.util.Arrays;
class Test 
{
  public static int matchStringLength(String str1, String str2)
  {
    return str1.length() - str2.length();
  }
}
public class Demo 
{
  public void printByStringLength() 
  {
    String names[] = {"abcde","abc","ab","abcd", "a"};
    Arrays.sort(names, Test::matchStringLength);
        
    System.out.println(Arrays.toString(names));
    } 
    public static void main(String args[])
    {
        new Demo().printByStringLength();
   }
}      


JDK 1.8 Features

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

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


2. Parallel Sort

import java.util.Arrays;
public class Demo 
{
  public static void main(String args[])
  {
    String fruits[] = {"guava","apple","banana"};
    String fruits1[] = {"guava","apple","banana"};

                                      // before JDK 1.8, using Arrays.sort()

    Arrays.sort(fruits);
    System.out.println(Arrays.toString(fruits));

                                       // with JDK 1.8, using Arrays.parallelSort()
    Arrays.parallelSort(fruits1);
    System.out.println(Arrays.toString(fruits1));
  }
}    


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.

import java.util.Calendar;                 // for Calendar class
import static java.util.Calendar.*;        // for static methods of Calendar class

public class Demo 
{ 
  public static void main(String args[]) 
  {
    Calendar calendar = Calendar.getInstance();
// populate calendar with individual set methods one-by-one as a separate statement
    calendar.set(YEAR, 2013);
    calendar.set(MONTH, APRIL);
    calendar.set(DATE, 10);
    calendar.set(HOUR, 8);
    calendar.set(MINUTE, 56);
    calendar.set(SECOND, 14);
    calendar.set(AM_PM, PM);

    System.out.println(calendar.getTime());

                                           // let us see the JFK 1.8 style
                                     // all set methods are set as one statement

    Calendar calendar1 = new Calendar.Builder()
                         .set(YEAR, 2013)
                         .set(MONTH, APRIL)
                         .set(DATE, 10)
                         .set(HOUR, 8)
                         .set(MINUTE, 56)
                         .set(SECOND, 14)
                         .set(AM_PM, PM)
                         .build();         // one semicolon here

    System.out.println(calendar1.getTime());
  }
}


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.

@FunctionalInterface
public interface Demo
{
  public abstract void display();
}

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

@FunctionalInterface
public interface Demo
{
  public abstract void display();
  public abstract void show();
}

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.

import java.util.*;
public class Demo
{
  public static void main(String args[])
  {
    List alphabets = Arrays.asList("A", "B", "C", "D");

    System.out.println("Printing with earlier JDK 1.5 for loop:");
    for(String str : alphabets)
    {
      System.out.print(str + "\t");
    }

    System.out.println("\nPrinting with latest forEach loop introduced with JDK 1.8 with lambda usage:");
     alphabets.forEach(str -> 
                        {  
                          System.out.print(str + "\t");
                        }
                      );
    }
}

JDK 1.8 Features

"-> " is known as lambda expression.

import java.util.*;
public class Demo
{
  public static void main(String args[])
  {
    List cities = Arrays.asList("Delhi","Hyderabad","Chennai","Mumbai");

    System.out.println("\n\nTo print the values earlier to Java 8:");
    for(String str : cities)
    {
      System.out.print(str + "\t");
    }   

    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"));

    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"));

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

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

13 thoughts on “JDK 1.8 Features”

  1. 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. 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 Comment

Your email address will not be published.