The largest Interview Solution Library on the web


« Previous | 1 | 2 | 3 | Next »

Java_8 New - Overview


What's new in JAVA 8?

JAVA 8 is a major feature release of JAVA programming language development. It's initial version was released on 18th March'14. With Java 8 release, java provided supports for functional programming, new JavaScript engine, new APIs for date time manipulation, new streaming api etc.

New features
  • Lambda expression - Adds functional processing capability to JAVA.
  • Method references - Referencing functions by their name instead of invoking them directly. Using functions as parameter.
  • Default method - Interface to have default method implementation.
  • New Tools - New compiler tools and utilities are added like jdeps to figure out the dependencies.
  • Stream API - New stream API to facilitate pipeline processing.
  • Date Time API - Improved date time api.
  • Optional - Emphasis on best practices, to handle null values properly.
  • Nashorn , JavaScript Engine - A JAVA based engine to execute JavaScript code.
Consider the following code snippet.

import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Comparator;

publicclassJava8Tester{

publicstaticvoid main(String args[]){
List<String> names1 =newArrayList<String>();
names1.add("Mahesh ");
names1.add("Suresh ");
names1.add("Ramesh ");
names1.add("Naresh ");
names1.add("Kalpesh ");

List<String> names2 =newArrayList<String>();
names2.add("Mahesh ");
names2.add("Suresh ");
names2.add("Ramesh ");
names2.add("Naresh ");
names2.add("Kalpesh ");

Java8Tester tester =newJava8Tester();

System.out.println("Sort using Java 7 syntax: ");
tester.sortUsingJava7(names1);
System.out.println(names1);

System.out.println("Sort using Java 8 syntax: ");
tester.sortUsingJava8(names2);
System.out.println(names2);
}
privatevoid sortUsingJava7(List<String> names){
//sort using java 7
Collections.sort(names,newComparator<String>(){
@Override
publicint compare(String s1,String s2){
return s1.compareTo(s2);
}
});
}

privatevoid sortUsingJava8(List<String> names){
//sort using java 8
Collections.sort(names,(s1, s2)-> s1.compareTo(s2));
}
}

Run the program. See the result.

Sort using Java 7 syntax:
[ Kalpesh Mahesh Naresh Ramesh Suresh ]
Sort using Java 8 syntax:
[ Kalpesh Mahesh Naresh Ramesh Suresh ]

Here sortUsingJava8() method uses sort function with a lambda expression as parameter to get the sorting criteria

« Previous | 1 | 2 | 3 | Next »


copyright © 2014 - all rights riserved by javatechnologycenter.com