Lambdas (Java 8)

Govinda Raj
4 min readDec 9, 2017

Greetings!

If you are a java developer then you must have heard of the word ‘Lambdas’. So what is this all about? Why this concept came into picture? Where I can use this new concept? like all these so many question might be going in your mind. So let’s start to know lambdas.

Why lambdas?

So basically it enables functional programming with OOP concept. You will get more readable and concise code in some certain situation, it also eliminates some boiler-plate code. It enables us to write APIs and libraries which are easy to use. It provides support for parallel processing.

So, let’s talk about functional programming. Why do we need functional programming? Aren’t OOPs concepts enough for us to do development?

Functional programming does not let us do any new thing what we have not done before. So what functional programming let us do is really, write better code, more readable code and so more maintainable code. Because if you think at end of the day, all we do is, we write machine code, write machine instruction and there is reason we don’t write assembly code because it hard to write and it’s hard to maintain that’s why we have all other programming paradigm. Think of functional programming as a other programming paradigm which let us write code which are elegant in certain situations. we are not going to be done by object oriented programming, java is inherently object oriented language and it will be continued to be so.

Let’s look on some problem where we apply functional programming-

Like in OOP, everything is an object, if you want to perform anything, you have to go through class or an object, so there is nothing which is isolated from these. so far it was not a problem but when you start design your code structure & all, then you realise, we should have some isolated way which can do this or that task. Basically what we do while designing, we think of all things that what we have and how we can use those. Just think that you want to write a method to greet someone or you just want to print greeting message on console, so what you do in OOPs-

The code will always print “Greet message” on console. so what if we want to print different message each time. So how to do with java? Think ………..One way to do in Java 7, I am not talking about java 8.

So guys, please notice above, what i have done is, I have passed behaviour in the method, I can easily create implementation of Greeting Interface. Don’t you think we are doing some extra work in above code like we are not passing the method, we are passing a thing, a object, a existing body. Like we should pass only method(action) to perform that task. So we can achieve this using lambda. So we can create a function which is called as lambda expression which do not belong to class or an object, they are just function which exist in isolation. And here is the best part — Those functions can be treated as values. So we know that, data act as values in java; we can assign it to variables similarly object act as value. So question is — Can we assign a block of code to a variable, which will act as a value?

Till Java 7, we were unable to do that, but now from Java 8 we can. So how do we write lambda expression in java?

aBlockOfCode=()->{ System.out.println(“Hello Reader!”)}

yeah, that’s it. If you know how to write method, you can write lambdas expression. Remove modifier, return type, method name and add ‘->’ symbol in a method as shown above. So you might be getting so many question in your mind. Like What is type of ‘aBlockOfCode’ variable? What is return type of the expression? How to execute lambda expression? Where should you write this block of code? What is the impact of the block in development procedure? Will you be able to access this block of code from anywhere? It’s going to be large answer but Iwill try to explain in shorts. Before answering those question let’s go through some more lambdas expressions.

What is type of ‘aBlockOfCode’ variable?

There is no any special type for ‘aBlockOfCode’ variable like FunctionType<>, but java let us do write a type for ‘aBlockOfCode’ variable, so that we can refer this variable by that type. So any guesses …

The type of ‘aBlockOfCode’ variable is a “Interface”, yeah you heard me right, it’s a Interface which will have exactly one method with same signature as of lambdas expression.

Okay. Now I know the type of lambdas expression variable but what about execution of that expression?

You can execute the expression just by typing —

aBlockOfCode.perform();

Yes my dear, That’s it. Here is the full example —

If you have any doubts on lambdas, please ask question in comment. I will explain more about Lambdas in next post.

--

--

Govinda Raj

Senior Software Developer. Tech Enthusiast and love coding. My portfolio: https://govinda-raj.github.io/