Lambda Expressions in Java 8 (Part 2)

Kasun Dissanayake
3 min readApr 20, 2019

--

Previously we learned the difference between Functional Programming and Object Oriented Programming. In this tutorial, we will be learning about how to write Lambda Expressions and the behavior of Lambda Expressions.

What are Inline Values?

Inline Values are values that you can define inline.

String name = "foo";
double pi = 3.14;

Here foo is a String which I have written inline. And when this is executing it will take this inline value and assign to the name. So name contains a value which is a String foo. We know data acts as a value in Java. You can assign it to variables. Similarly, Objects act as a value in Java. You can assign an instance of Object to a variable.

Can we assign a block of code to a variable as a value?

aBlockOfCode = {
...
...
...
}

What is the proper way to write a block of code in Java? It is using a method!!. You create a method which contains a block of code. It has arguments and a return type. This is the way to do it. Imagine if you could take a method and assign it to a variable. Now method becomes a thing that was assigned to a variable.

Let’s say I have this perform method.

public void perform(){
System.out.println("Welcome...");
}

I am going to assign this block of code to a variable.

aBlockOfCode = public void perform(){
System.out.println("Welcome...");
}

Is this possible? Yes! this is possible in Java 8 using Lambda. You can write a Lambda expression which does just this. You can take this variable of aBlockOfCode and pass it around and do so many things. Let’s see how to write Lambda expressions correctly. This code block has a lot of extra things that you don’t need.

Public — public make sense when a function is a part of a class. But if a function exists in isolation doesn’t make sense to call is public. Remove the public keyword.

aBlockOfCode = void perform(){
System.out.println("Welcome...");
}

Name — when you assign a string to a variable what is the name that you refer the String by? It’s the name of the variable. You don’t need to give another name. Here also the same. Remove the name.

aBlockOfCode = void (){
System.out.println("Welcome...");
}

Return Type — Java compiler can look at the code and figure the return type of the code block. You do not need to provide return type on the beginning of the code block in Lambda Expressions. Remove the return type.

aBlockOfCode = (){
System.out.println("Welcome...");
}

Finally, you should add one symbol to complete the Lambda expression. You need to add “->” symbol here.

aBlockOfCode =() -> {
System.out.println("Welcome...");
}

Now you have got your Lambda Expression. If your body of the Lambda Expression is just one line you can actually remove the curly braces.

aBlockOfCode = () ->  System.out.println("Welcome...");

NOTE: This is only if you have just one line of code.

Let’s write a few more Lambda Expressions.

Example 1 :

printFunction = () -> System.out.println("Welcome..");

Now you can pass around this printFunction variable. You can send it to another method as an argument. For instance, I can call the print method and pass this printFunction function.

print(printFunction);

Furthermore, you can just pass the Lambda expression to the print method too.

print(() -> System.out.println("Welcome.."));

NOTE: Think back to the Example of inline Strings and Numbers. Just like you can pass an inline String value to a method, you can pass in an “inline” lambda expressions too.

Example 2:

Function with parameters.

doubleNumberFunction = (int a) -> a*2;

Here Java compiler detects what is the return type of the function and assign to the variable. We don’t need to specify the return type of the function.

NOTE: It is actually invalid to specify the return keyword when you have a one-liner expression without “{}”

Example 3:

Function with multiple parameters.

addFunction = (int a, int b) -> a+b;

Example 4:

Function with function body(Multiple lines).

safeDevideFunction = (int a, int b)->{
if(b==0) return 0;
return a/b;
};

We will execute these examples and create Interfaces using Lambda expressions in the next tutorial.

Thank You!

--

--

Kasun Dissanayake
Kasun Dissanayake

Written by Kasun Dissanayake

Senior Software Engineer at IFS R & D International || Former Software Engineer at Pearson Lanka || Former Associate Software Engineer at hSenid Mobile

Responses (1)