Jave 8 New Features

JAVA 8 New Features With Examples

Introduction to Java 8:

JAVA 8 (jdk 1.8) provide support for functional programming, new JavaScript engine, new API’s for Date time manipulation, new streaming API, etc

Java 8 New Features:

There are a lot of features added to java 8, few of them are mentioned below:

1. Lambda expression : It provides functional processing capability to java.

2. Method references : Instead of calling the functions directly, reference them by their name. Using functions as parameter.

3. Default method : It introduces a default method implementation in an Interface.

4. Stream API : A new stream API is introduce to facilitate the pipeline processing.

5. Date Time API : Improved Date Time API.

6. Optional : It emphasis on best practices to handle null values.

7. Nashorn, JavaScript Engine : It is java based engine to execute JavaScript code.

NOTE: Apart from these new features, a lot of feature enhancements are done at both compiler and JVM level.

Lambda Expressions:

Lambda expression facilitates the functional programming, and simplifies the development a lot.

Syntax: parameter -> expression body

Following are the important characteristics of a lambda expression:

a) Optional type declaration: There is no need to declare the type of a parameter. The compiler will inference the same from the value of parameter.

Example:

//Operation without type declaration
 CalculatorOperation subtraction = (a, b) -> a - b;

b) Optional parenthesis around parameter: There is no need to declare parenthesis around single parameter, but in case of multiple parameter parenthesis is required.

Example:

//Operation without parenthesis
 Message subtractInfo = msg -> System.out.println(msg);

c) Optional Curly braces: There is no need to put curly braces around expression if expression contains single statement.

Example:

//Operation without curly braces
 CalculatorOperation multiplication = (double a, double b) -> a*b;

d) Optional return keyword: The compiler will automatically return the value if the body has single expression which will return the value. Curly braces are required to indicate that expression will returns a value.

Example:

//Operation with return statement along with curly braces
 CalculatorOperation multiplication = (double a, double b) -> {return a*b;};

Points to be considered:

  • Lambda expressions are used primarily to define inline implementation of a functional interface, i.e., an interface with a single method only.
  • Lambda expressions gives a very simple yet powerful functional programming capability to Java.
  • Lambda expressions can also be used to refer final variables.

Below is the example of basic implementation of lambda expression. In this example we have two different interface, one is for calculator operation and other one is to display input message. Also there are two more classes; one utility class and other is main class.

The CalculatorOperation Interface with method operation.


/**
*
*/
package com.abhi.lambdaexpression;

/**
* @author Abhinav
*
*/
public interface CalculatorOperation {

/**
* @param valA
* @param valB
* @return
*/
double operation(double valA, double valB);

}

The Message Interface with method showMessage.


/**
*
*/
package com.abhi.lambdaexpression;

/**
* @author Abhinav
*
*/
public interface Message {

/**
* @param msg
* @return
*/
void showMessage(String msg);
}

The Calculator utility class with method operations.


/**
*
*/
package com.abhi.lambdaexpression;

/**
* @author Abhinav
*
*/
public class Calculator {

private Calculator(){

}

/**
* @param valA
* @param valB
* @param calcOperation
* @return
*/
public double operations(double valA, double valB, CalculatorOperation calcOperation) {

return calcOperation.operation(valA, valB);
}

/**
* @return
*/
public static Calculator getInstance(){
return new Calculator();
}

}

The executor class CalculatorMain with different declarations of lambda expressions.


/**
*
*/
package com.abhi.lambdaexpression;

/**
* @author Abhinav
*
*/
public class CalculatorMain {

final static String ADDITION = "1 + 1 = ";
final static String SUBTRACTION = "4 - 1 = ";
final static String MULTIPLY = "2 * 2 = ";
final static String DIVIDE = "10 / 2 = ";
/**
* @param args
*/
public static void main(String[] args) {

Calculator calc = Calculator.getInstance();

//Operation with type declaration
CalculatorOperation addition = (double a, double b) -> a + b;

//Operation without type declaration
CalculatorOperation subtraction = (a, b) -> a - b;

//Operation with return statement along with curly braces
CalculatorOperation multiplication = (double a, double b) -> {return a*b;};

//Operation with return statement along with curly braces
CalculatorOperation division = (double a, double b) -> a/b;

//Operation with parenthesis
Message addInfo = (msg)-> System.out.println(msg);

//Operation without parenthesis
Message subtractInfo = msg -> System.out.println(msg);

//Operation with parenthesis
Message info = (msg) -> System.out.println(msg);

addInfo.showMessage("Addition");
System.out.println(ADDITION+ calc.operations(1, 1, addition));
subtractInfo.showMessage("Subtract");
System.out.println(SUBTRACTION+ calc.operations(4, 1, subtraction));
info.showMessage("Multiplication");
System.out.println(MULTIPLY+ calc.operations(2, 2, multiplication));
info.showMessage("Division");
System.out.println(DIVIDE+ calc.operations(10, 2, division));

}

}

You can checkout/clone the lambda expression example repository from the mentioned path – https://github.com/erabhinavrana/my-java8-learning

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s