Java 8

Java8 – Introduction

Java 8 is the most awaited and is a major feature release of Java programming language. This is an introductory tutorial that explains the basic-to-advanced features of Java 8 and their usage in a simple and intuitive way.

This tutorial will be useful for most Java developers, starting from beginners to experts. After completing this tutorial, you will find yourself at a moderate level of expertise in Java 8, from where you can take yourself to next levels.

Prerequisites

Knowledge of basic Java programming language is the only prerequisite for learning the concepts explained in this tutorial.

Overview

JAVA 8 is a major feature release of JAVA programming language development. Its initial version was released on 18 March 2014. With the Java 8 release, Java provided supports for functional programming, new JavaScript engine, new APIs for date time manipulation, new streaming API, etc.

New Features

  1. Lambda expression − Adds functional processing capability to Java.
  2. Method references − Referencing functions by their names instead of invoking them directly. Using functions as parameter.
  3. Static method – Interface to have static method implementation.
  4. Default method − Interface to have default method implementation.
  5. New tools − New compiler tools and utilities are added like ‘jdeps’ to figure out dependencies.
  6. Stream API − New stream API to facilitate pipeline processing.
  7. Date Time API − Improved date time API.
  8. Optional − Emphasis on best practices to handle null values properly.
  9. 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;

public class Java8Fearure {

   public static void main(String args[]) {
   
      List<String> names1 = new ArrayList<String>();
      names1.add("Rakesh");
      names1.add("Amir");
      names1.add("Suresh");
      names1.add("Kapilan");
      names1.add("Ganesh");
		
      List<String> names2 = new ArrayList<String>();
      names2.add("Rakesh");
      names2.add("Amir");
      names2.add("Suresh");
      names2.add("Kapilan");
      names2.add("Ganesh");
		
      Java8Tester tester = new Java8Tester();
      System.out.println("**** Sort using Java 7 ****");
		
      tester.sortUsingJava7(names1);
      System.out.println(names1);
      System.out.println("**** Sort using Java 8 ****");
		
      tester.sortUsingJava8(names2);
      System.out.println(names2);
   }
   
   //sort using java 7
   private void sortUsingJava7(List<String> names) {   
      Collections.sort(names, new Comparator<String>() {
         @Override
         public int compare(String s1, String s2) {
            return s1.compareTo(s2);
         }
      });
   }
   
   //sort using java 8
   private void sortUsingJava8(List<String> names) {
      Collections.sort(names, (s1, s2) -> s1.compareTo(s2));
   }
}
Output of the program
**** Sort using Java 7 ****
[Amir, Ganesh, Kapilan, Rakesh, Suresh]
**** Sort using Java 8 ****
[Amir, Ganesh, Kapilan, Rakesh, Suresh]

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

About the Author: Elavarasan PK

Technical Specialist, Intersoft Data Labs