Functional Programming

Lets’s see the definition first:

Functional programming is a programming paradigm in which we try to bind everything in pure mathematical functions style. It is a declarative type of programming style. Its main focus is on “what to solve” in contrast to an imperative style where the main focus is “how to solve”.

What are the benefits of Functional Programming?

  1. Functional programming make the code easier to understand by encapsulating the major components , involves minimizing the number of major components.
  2. It allows you to run any function in isolation( without classes and objects) , like we know in object oriented programming we need to have objects and classes to run any function .
  3. In , functional programming there is no loops , here logic are implemented using only recursion.
  4. Here we have , constant values we do not change values once initialized, instead we create new variables.This eliminates any chances of side effects because any variable can be replaced with its actual value at any point of execution. State of any variable is constant at any instant.

5. With the use of functional programming testing and debugging is also become easier as, they only take arguments and return output , no function call , find global variables , etc.

Now, Lets see how we use functional programming in java :

We use Lambda expression to apply functional programming in our code.First discuss. What is lambda?

Lambda expression expresses instance of functional interfaces(A interface with single abstract method interfaces called as functional interface).Lambda expressions implement only abstract methods that’s why they implement functional interfaces.

Image for post
Image for post
Lambda Example
Normal Function

Above , an example of lambda function, and a normal function

Let’s see how lambda expression is different and more efficient than our normal function.What are the things which we can remove or add from functions and convert it into a lambda expression.

  1. Public() — In normal function we have public. For what ? we use public in a function to make it visible to other classes , But functional programming allows us to make function in isolation without classes and objects , so there is no need to write public in front of lambda expression.
  2. void()- In java 8 the compiler is smart enough to understand the return type of your function only by seeing your code .So , no need of void now.
  3. ()-> sign to start any lambda expression , in parenthesis you can add parameters .
  4. Interfaces()- we need to implement interfaces to run any lambda expression like in the above example I created sum interface for my greet lambda expression .

Note: one thing to keep in my that if you have a lambda expression with no parameter or void then the interface you created for that lambda should have only one function with the same return type.

Lambda Expression can be of zero parameter :-() ->,

one parameter :- ( int a) ->

two parameter :- (int a ,int b) ->

ex : — () -> System.out.println(“We are here to learn”); // this is inline lambda , where you can remove curly brackets

ex: — addFun add( int a , int b)->{ //mulitiline lambda

System.out.println(“We are here to learn”);


here , sum is the type of lambda(implemented interface) and greet is the name(variable name).

Calling of lambda is done using the name and interface method

Ex. → add.second(4,5);

second is the method name which has the same return type as lambda expression and add is the variable name and 4,5 are parameters.

We have different function interfaces to add value or implement our lambda expression

Ex consumer, supplier , BiFunction etc…

Thanks for giving your valuable time

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store