Table of Contents
What is Functional Programming?
Functional programming is a programming style that allows blocks of code to be passed as argument to methods for executing the functionality. The block of code can exist without any class and can be assigned to a variable of type functional interface.
Lambdas allow us to create independent blocks of code without being associated with a class.
In Java 8, some ready functional interfaces are introduced which can be used for functional programming.
Let’s first look at the traditional way of writing Java code.
We have 3 methods,
- one of them does not return anything but prints a message
- the second one takes 2 integers as input and returns their sum
- the third one takes 2 integers as input and returns their product.
Below are the 3 methods
public void printMessage() {
System.out.println("Hello I am using the traditional way to print the message");
}
public int addNumbers(int num1, int num2) {
return num1 + num2;
}
public int multiplyNumbers(int num1, int num2) {
return num1 * num2;
}
We are calling this method from the main method as below
FPClient fpClientTraditional = new FPClient();
fpClientTraditional.printMessage();
System.out.println(“Adding numbers using the traditional way : ” + fpClientTraditional.addNumbers(10, 6));
System.out.println(“Multiplying numbers using the traditional way : ” + fpClientTraditional.multiplyNumbers(10, 6));
The output appears as below

Now in the next step we will gradually modify this code to introduce Lambda.
Let’s convert the first method where we are printing a message.
Let us create an interface as below which will have one abstract method.
public interface GenericMessage {
public void printString();
}
We will create an anonymous object using this interface and create a runtime implementation of the abstract method.
//using anonymous class
new GenericMessage() {
@Override
public void printString() {
System.out.println(“Printing using anonymous class”);
}
}.printString();
When we run the method, we can see this message getting printed. Here instead of creating a separate method, we are directly implementing the printString() method of the interface and printing our message.

Now let’s improve the code. Java8 introduced another feature called lambda.
What is lambda?
Lambda is block of code used to implement a method defined by a functional interface. Lambdas help us disassociate a set of instruction from object.
What is functional interface?
Functional interface is an interface with one and only one abstract method. It is recommended to annotate the interface with @FunctionalInterface annotation to avoid accidental modification by adding second abstract method.
What will happen if we do not annotate the interface with @FunctionalInterface annotation?
If we create an interface with only one abstract method, then we can use it as functional interface even if we do not annotate the interface. However, let’s think of a scenario where this interface is being used by some external project. If we add a new abstract method in this interface, our current project will compile however, the one using this interface as functional interface with fail to compile. Adding the annotation @FunctionalInterface restricts us from adding a second abstract method. Thus, if we try to add a new abstract method to an interface annotated with @FunctionalInterface, compiler will immediately revolt against us.
How to convert the above code to lambda expression?
Now let us convert the above scenario to a lambda expression.
Step 1 : Create a functional Interface
@FunctionalInterface
public interface GenericMessage {
public void printString();
}
Step 2: Create a lambda expression
//lambda
GenericMessage genericMessage = () -> {
System.out.println(“Hello I am using the lambda to print my first message”);
};
GenericMessage genericMessage1 = () -> {
System.out.println(“Hello I am using the lambda to print my second message”);
};
Let’s compare this code snippet with our previous method. We have removed the method declaration – public void printMessage(). We are just keeping the braces. Then the -> is a mandatory syntax followed by the method body. Even as the current block of code has only one line, so we can remove the curly braces and write it in a single line as below
() -> System.out.println(“Hello I am using the lambda to print my second message”);
Now this entire thing is assigned to a variable genericMessage.
In Java, every variable must be having a datatype. So, what would be the datatype of this variable.
Here comes the functional interface. Keep in mind that it is an interface with ONE and ONLY ONE abstract method – printString in this case. We are using it as the datatype of the variable genericMessage. The block of code which we have created just now and is assigned to variable, is the actual implementation of abstract method – printString.
In the above examples, we have created 2 different implementations of the abstract method.
Step 3 : Executing the block of code
genericMessage.printString();
genericMessage1.printString();
The output is as below

Re-using same functional interface to compute sum and product of 2 numbers using lambda expression
The following 4 steps will guide you to assign different lambda expression to same FunctionalInterface.
Step 1: Let’s create a new functional interface with an abstract method which accepts two integers and returns another integer
@FunctionalInterface
public interface NumberCalculator {
public int compute(int a, int b);
}
Step 2: Now let’s create lambda expression to sum up two integers
(a, b) -> a + b
Step 3: Now we are assigning it to a variable whose datatype is NumberCalculator
The lambda expression can only be assigned to a functional interface whose abstract method has same contract as the lambda expression.
NumberCalculator sumNumbers = (a, b) -> a + b;
Similarly, we are creating a similar variable for multiplying 2 integers.
NumberCalculator multiplyNumbers = (a, b) -> a * b;
Step 4: Now to execute the lambda, expression and print the result the following statements are inserted.
System.out.println(“Adding of 2 numbers using lambda expression : ” + sumNumbers.compute(10, 6));
System.out.println(“Product of 2 numbers using lambda expression : ” + multiplyNumbers.compute(10, 6));
The result appears as below

2 thoughts on “Functional Programming – Basics”