A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

JAVA 8

Java 8

Java 8


Java 8 introduces in 18th march 2014, It comes for concise the code with the help of functional programming. the new features in java 8 are:-



Lambda expressions:-

  • We can use functional programming with the help of Lambda expressions it is very easy to use.

  • It implements a specific interface called functional interface.

  • Lambda expressions is a function that does not have any name,return type and modifer or it is a nameless function so we called Lambda expressions is an Anonymous function.

  • A function that can be created without belonging to any class.

Syntax of lambda expression-





(parameters) -> function body;


 

Code Without Lambda Expression to print Hello world





public class Test
{
  public static void main(String args[])
  {
    System.out.println("Hello World..");
  }
} 


 

Code With Lambda Expression to print Hello world





import java.util.function.*;
interface HelloWorldLambdaExpression
{
  void print();
}
public class HelloWorld 
{
  public static void main(String[] args) 
  {
    HelloWorldLambdaExpression obj=()->{System.out.println("Hello World...");}; // lambda expression to define the calculate method 
    obj.print();  // parameter passed and return type must be same 
  }
} 



 

Code Without Lambda Expression to print Addition value





public class Test
{
  public static void main(String args[])
  {
    int a=10,b=20;
    System.out.println(a+b);
  }
} 


 

Code Witht Lambda Expression to print Addition value





import java.util.function.*;
interface Sum
{
  int sumCalculation(int a, int b);
}

public class ExampleOfFunctionalInterfaceAnnotation 
{
  public static void main(String[] args) 
  {
    int m=10; int n=20;
    Sum sum=(int a, int b)->a+b; // lambda expression to define the calculate method
    int result=sum.sumCalculation(m, n);  // parameter passed and return type must be same
    System.out.println("Result: "+result); 
  }
}


 

LEARN TUTORIALS

.

.