Java 8: What’s New In It ?

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 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 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, 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 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 helps to write less code in a more readable way. 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.

2) 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 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
Arrays.sort(strArr);
System.out.println(Arrays.toString(strArr));
//parallelSort
Arrays.parallelSort(strArr1);
System.out.println(Arrays.toString(strArr1));
}
}

The performance with parallelSort() can be seen when the number of arrays to sort is very large. 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();
//NORMAL
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);
System.out.println(calendar.getTime());

//USING JDK1.8
Calendar calendar1 = new Calendar.Builder()
.set(YEAR, 2012)
.set(MONTH, MARCH)
.set(DATE, 09)
.set(HOUR, 7)
.set(MINUTE, 55)
.set(SECOND, 13)
.set(AM_PM, PM)
.build();
System.out.println(calendar1.getTime());
}
}

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, 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 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.



Author: InApp
We are a custom software development company offering Testing Services, Application Development, Mobility Solutions & more. Customers: Startups - Fortune 500

Leave a Reply

eight + nine =