DEV Community

Cover image for Functional Programming in Java
salmanetamo
salmanetamo

Posted on

Functional Programming in Java

This will be a quick intro to Functional Programming in Java. In order to understand this tutorial, you should be familiar with the basics of writing code in Java. If you want to follow along and try some of the code, you may use your favorite IDE; I personally use IntelliJ.

What is Functional Programming in Java?

Simply put, Functional Programming is a style of programming that leverages functions. You can think of these functions as simple mathematical functions like f(x) = x + 2.

Function<Integer, Integer> f = x -> x + 2;
System.out.println(f.apply(4));  // Prints 6
Enter fullscreen mode Exit fullscreen mode

A very common description of Java is that it is an Object Oriented Programming language, and any Java developers should be comfortable with OOP constructs such as classes and objects. In Functional Programming, focus is less about objects and more about the functions.

The predictability nature of Functional Programming allows us to write code that is more easily testable and with no desired side effects. This is because the functions, in Functional Programming, are pure(they always return the same output for a given input) and enable immutability and referential transparency.

How to use Functional Programming in Java?

In Java, Functional Programming is enabled through the usage of Lambdas, which were introduced in Java 8. Lambdas are implementations on functional interfaces(interface with a single abstract method), and are really the ones that play the role of the “functions” in Functional Programming. They can be assigned to variables, can be passed as parameters to other functions or can even be returned from other functions.

Why do we need Functional Programming in Java?

There are several reasons why we should care about Functional Programming as we strive to write concise, readable and efficient code.

  • Lazy evaluation: With lazy evaluation, an expression is only executed when its value is needed, which both saves time and keeps us from doing the same computation multiple times. See the output of the example below to illustrate this
var numbers = List.of(5, 2, 7, 8);
Stream<Integer> numbersPlusTwo = numbers.stream().map(number -> {
    System.out.println("Printing number: " + number);
    return number + 2;
});
System.out.println("I get printed first");
System.out.println("Numbers plus two: " + numbersPlusTwo.collect(Collectors.toList()));
Enter fullscreen mode Exit fullscreen mode

The output:

// I get printed first
// Printing number: 5
// Printing number: 2
// Printing number: 7
// Printing number: 8
// Numbers plus two: [7, 4, 9, 10]
Enter fullscreen mode Exit fullscreen mode
  • Immutability: Functions in Functional Programming do not modify state, and only act on the inputs they are provided. This avoids unexpected behavior and makes it easier to debug the code.
  • Readability and Conciseness: With Functional Programming, we write much more concise code, which is generally more readable.

This was a brief look at Functional Programming in Java, its usage, and its advantages. With functions being treated as “first-class citizens”, we get to achieve a lot and more efficiently in Java. Functional Programming is not meant to oppose Object Oriented Programming but really augments it, and combining them together in your programs is the more obvious way to go.

Top comments (0)