Tuesday, May 26, 2020

Java 8 Lambda Expressions with examples and Rules

Java 8 Lambda Expressions, Oracle Java Tutorial and Material, Oracle Java Learning, Oracle Java Exam Prep

Java 8 is first released in 2014 and introduced lot of new features. Lambda expressions are the most significant change in java 8 version.

As of java 7, Oracle/Sun people have given impartance to the object oriented programming languages but in java 8, they have introduced functional programming to compete with other programming languages such as Scala, C# etc.

What is Lambda Expression?


Any function which is having no name is called as Lambda expression. Which is also called as anonymous function.

Rules:

1) function should not have access modifier
2) Should not have any return type (even void also not allowed)
3) Should not have name for function
4) Should use arrow symbol "->"

We will see now a few examples how to convert normal java fucntions to lambda expression.

Example 1:

Before java 8:

public void print() {
  System.out.println("Hello World");
 }

In java 8:

() -> {
  System.out.println("Hello World");
   };

Please observe here, we have remvoed fucntion access modifier (public), return type(void) and method name (print) in the lambda expression and added -> symbol. Note: If method body has only statement then curly braces are optional. Curly braces are mandetory if multiple statements are present in the method body. we can rewrite the above lambda expression as below.

() -> System.out.println("Hello World");

Example 2:

Before java 8:

public void sum(int a, int b) {
  System.out.println("sum :" + (a + b));
 }

In java 8:

(int a, int b) -> System.out.println("sum :" + (a + b));

Example 3: Finding the length of string Before java 8:

public int getLength(String value) {
  return value.length();
 }

In java 8:

(String value) -> {
   return value.length();
  }; 

Java 8 Lambda Expressions, Oracle Java Tutorial and Material, Oracle Java Learning, Oracle Java Exam Prep

Lambda Expressions Thumb Rules:


1) Parameters are optional. It can be zero or more.

() -> {System.out.println("Hello World");};
(int a) -> {System.out.println("value "+a);};

2) If no parameters are available then need to use empty parenthesis ().

() -> {System.out.println("Hello World");};

3) If we have multiple parameters then need to separate them with comma(,)

(int a, int b) -> System.out.println("sum "+(a+b));

4) if body has only statement then curly braces are optional.

(int a) -> System.out.println("value "+a);

5) if body has more than one statement then curly braces are mandetory.

() -> {
   System.out.println("Hello World");};
   System.out.println("value "+a);
    };
 
6) parameter type(s) is optional. No need to declare manually because compile can expect based on the context. We will disscuss more in the next post.

(a) -> System.out.println("value "+a);
(a, b) -> System.out.println("sum "+(a+b));

7) If only one parameter is available then parenthesis are optional.

(int a) -> {System.out.println("value "+a);};

The above can be rewritten as a -> {System.out.println("value "+a);};

Related Posts

0 comments:

Post a Comment