Java Functional programming with Lambda Expressions

Hi guys today I am gonna talk about Functional programming in Java.

It is clear that we can do anything that we need to do with Java using Object Oriented programming, also functional programming doesnot let you do anything new that you couldnot have done before.

Then Why Functional programming important ?? 
What functional programming let you do is write more readable and maintainable code. Think of functional programming as another programming paradigm which lets you write more elegant code when in situations that are not gonna go with object oriented programming.

What is Lambda Expressions ??

Lambdas lets you create entities which are just functions which is known as Lambda Expresssions. 
In object oriented programming, a function should belong to a class. But Lambda Expresssions are the functions that exist in isolation (that doesnot belong to a class).

πŸ‘‰This could be kind of confusing for you if you were used to Object oriented programming for so long and haven't done functional programming before. But keep reading... you will understand the concept behind functional programming at the end of this blog post.

Function as values

Let me explain what function as values means,
As shown in the above image we can have inline values in Java. In the example you can see that a string value has been assign to a name variable inline. Which means the name contains a string value "Sandani". So you can understand that data can be assigned to variables in Java. Simillarly an instance of an object also can be assigned to a variable. And the question is Can we assign a block of code to a variable or as a value??

  aBlockOfCode = {
       ...
       ...
    }

Is it possible to have a scenario as above in Java?

So as you all know method contains a block of code and method can have input arguments and also a return type.


So you can see in the above example, the perform method is a public method which returns a void.


So think of a scenario as above. Is it possible??

The answer is YES. After Java version 8 it was possible to have these kind of scenarios using Lambda Expressions. 

But hold on there is a standard way of writing a Lambda Expression. Next I will explain you on how to write a lambda expression.

In the above syntax there are lot of extra things which we don't need. 
  • Public keyword
"public" keyword make sense when a function/method is a part of a class to define accessability to that specific method.As I explained before, if the function exists in isolation (not belong to a class) having accesss modifiers in the above syntax is useless. Which means we don't need "public" keyword when writing lambda expressions.
  • Function name
When a String is assigned to a variable, How do you gonna refer to that variable?? Its obviously
should be the variable name right? So then if the function is assigned to a variable in lambda why do we need another name for the function/method. Which means we can eliminate the function name also from the syntax.
  • Return type
If the function given above doesnot return any value, is it a must to mention the return type? I mean we can check the block of code and figure out what kind of a value will the function return. So Java is now smart enough to identify the return type by looking at the block of code.Which means we can eliminate the return type also from the syntax.

πŸ‘€Now let us look what is left

πŸ‘‰Now let us look what the correct syntax of writing Lambda Expressions.


That's it for today, Hope you got the idea of Functional Programming in Java.

Hope to see you guys with a new blog post soon.
Thank You.





Comments