What’s New In Java 8?

What’s New In Java 8?

Oracle launched a new version of java Jdk1.8 with a lot of features. Some of the important features are provided below.

1) Lambda

JDK 1.8 allows you to create Lambda functions. Lambda functions will become a powerful concept once integrated with JAVA. Lambda refers to an anonymous function in a programming language. Lambda function, generally known as Lambda expression, is a function but without a name. It is very much used in languages like Python and Ruby (which is borrowed from LISP) etc. An anonymous function (lambda function) does not carry a name, access specifier, access modifier, parameters, etc. It is just to simplify the code. Lambda function is very convenient to use in the same place where we write a function. If you would like to use the function only once, the lambda function is the more convenient way. It reduces typing a lot of code because the function code is written directly where we use the function and the programmer need not go to another part of the application to see the function code (if required to modify).
To write lambda function in Java, we use -> symbol.

package com.inapp.java

import java.util.*;
public class LambdaExpressionDemo {
public static void main(String args[]) {
List<String> alphabets = Arrays.asList(“A”, “B”, “C”, “D”);
System.out.println(“NORMAL “);
for(String str : alphabets) {
System.out.print(str + “\t”);
System.out.println(“\nUSING LAMBDA :”);
alphabets.forEach(str -> { System.out.print(str + “\t”); } );

Java 8 lambda expressions help to write less code in a more readable way. The lambda expression consists of two parts, the left of the lambda arrow and the right of the lambda arrow. The 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.

2) Functional Interfaces

An interface containing only one abstract method is known as a functional interface. For example, the java.lang. A 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.

public interface FunctionalInterfaceDemo {
public abstract void display();

3) Parallel Sort

This is a sorting mechanism introduced in jdk1.8 for better performance.

package com.inapp.java

public class ParallelSortDemo {
public static void main(String args[]) {
String strArr[] = {“java”,”php”,”dotNet”};
String strArr1[] = {“java”,”php”,”dotNet”};
// Normal sort

The performance with parallelSort() can be seen when the number of arrays to sort is very large. The same sorting can also be done using List or Set instead of arrays.

4) Addition of Calendar

In normal way each set() method added as a separate statement, but in JDK 1.8,Calendar.Builder is used to instantiate Calendar instance and all set methods are used as a single statement. Semicolon is given only after build() method.

package com.inapp.java
import java.util.Calendar;
import static java.util.Calendar.*;

public class CalendarDemo {
public static void main(String args[]) {
Calendar calendar = Calendar.getInstance();
calendar.set(YEAR, 2012);
calendar.set(MONTH, MARCH);
calendar.set(DATE, 09);
calendar.set(HOUR, 7);
calendar.set(MINUTE, 55);
calendar.set(SECOND, 13);
calendar.set(AM_PM, PM);

Calendar calendar1 = new Calendar.Builder()
.set(YEAR, 2012)
.set(DATE, 09)
.set(HOUR, 7)
.set(MINUTE, 55)
.set(SECOND, 13)
.set(AM_PM, PM)

5) Replacement of Permanent Generation with Metaspace

JDK 1.8 removes Permanent Generation (PermGen) space and in its place introduced Metaspace. In HotSpot VM, the PermGen is used to give OutOfMemoryError due to depletion of space, which may sometimes cause memory leaks while loading and unloading a J2EE application. From JDK 1.8, most of the memory allocation for storing metadata is done through native memory. The existing classes used to retrieve the metadata of a class no more works with metaspace. A flag is introduced to limit the maximum memory allocation for metaspace – MaxMetaspaceSize. If this flag is not set, the metaspace will be 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 the removal of PermGen space, we cannot configure the space through XX:PermSize & -XX:MaxPermSize.

6) Small VM

The goal of JDK1.8 is to have a VM which is no more than 3Mb by allowing some features to be excluded at build time. The motivation behind this is to allow the JVM to run on small devices which have very strict static and dynamic memory-footprint requirements.

Have questions? Contact the technology experts at InApp to learn more.

Insights & News