An Easy Explanation of Pure Functions in Java

Within the domain of functional programming, the principle of pure functions plays a crucial role in crafting clean, efficient, and error-resistant code. This article delves into the concept of pure functions, with a particular focus on their application in Java.

What are Pure Functions?

A function is considered pure if it adheres to the following two fundamental principles:

  • The return value of the function is exclusively determined by the input parameters passed to the function. This implies that, given the same input values, the function will consistently yield the same output, ensuring predictability and reliability in the code.

  • The execution of the function does not generate any side effects. In other words, the function does not modify any external state or data, nor does it interact with the outside world (e.g., reading from or writing to a file, altering global variables, etc.). This attribute of pure functions aids in minimizing the potential for bugs and makes the code more comprehensible, testable, and maintainable.

To gain a clearer understanding of pure functions, let's examine a few examples:

Example 1: A Simple Arithmetic Function

public class Main {
    public static int add(int a, int b) {
        return a + b;

    public static void main(String[] args) {
        System.out.println(add(5, 3));

In the example above, the function add() is a pure function. It takes two parameters and returns their sum. The output of this function depends entirely on its input parameters, and it does not produce any side effects, such as modifying external states or data.

Example 2: A Function with Side Effects

public class Main {
    static int a = 5;

    public static int add(int b) {
        a = a + b;
        return a;

    public static void main(String[] args) {

In contrast, in this example, the function add() is not a pure function. Although it returns the sum of a and b, it also modifies the external state (the static variable a). Additionally, the output of this function is not solely determined by its input parameters, as the same input produces different outputs on subsequent calls. This example demonstrates what can happen when using functions with side effects; if the user does not read the code correctly or fails to understand what is happening, undesirable outcomes may occur with this type of function.

Example 3: A Function Interacting with the Outside World

package dev.lehnertchristian;


public class Main2 {
    public static String readFile(String fileName) {
        try {
            InputStream is = Main2.class.getClassLoader().getResourceAsStream(fileName);
            if (is == null) {
                throw new FileNotFoundException("Resource file not found: " + fileName);
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            try {
                StringBuilder sb = new StringBuilder();
                String line = br.readLine();

                while (line != null) {
                    line = br.readLine();
                return sb.toString();
            } finally {
        } catch (IOException e) {
            System.err.println("An error occurred while reading the file.");
            return null;

    public static void main(String[] args) {
        String content = readFile("test.txt");
        if (content != null) {

In this example, the function readFile() reads data from a file, which is an interaction with the outside world. Therefore, it is not a pure function, even though it does not modify any external data or state.

Through these examples, we can see that pure functions in Java provide a predictable and reliable way to write code, as they always produce the same output for the same input and do not produce any side effects.

Benefits of Pure Functions

  1. Predictability: Pure functions greatly enhance predictability in code. Since the output of a pure function is solely dependent on its input, anticipating the behavior of the function becomes much easier. This predictability allows developers to write more reliable and maintainable code, as they can be confident that the function will always produce the same output for the same input, without any unexpected side effects.

  2. Testability: Pure functions significantly simplify the testing process. When working with pure functions, you don't have to worry about mocking external dependencies or states, as the function's behavior is completely isolated from external factors. This isolation makes it easier to write test cases and ensures that the tests accurately reflect the function's behavior, leading to more robust and reliable code.

  3. Concurrency: In Java, pure functions facilitate safer concurrent programming. Since pure functions do not produce any side effects, the risks of race conditions and deadlocks are significantly reduced. This is particularly important in multithreaded applications, where the potential for concurrency-related issues is higher. By using pure functions, developers can more easily write concurrent code that is both efficient and less prone to errors.

  4. Reusability: Pure functions, by virtue of their isolated nature, are highly reusable across different parts of an application. Because they do not rely on external state or produce side effects, pure functions can be easily repurposed and used in various contexts without the need for modification. This reusability not only promotes code modularity but also reduces the likelihood of introducing bugs when reusing code in different parts of an application.


Pure functions in Java offer a multitude of benefits, including enhanced predictability, easier testing, safer concurrent programming, and higher code reusability. By adhering to the principles of pure functions, developers can write code that is more efficient, reliable, and maintainable. The concept of pure functions, which is rooted in functional programming, can be a powerful tool in creating clean and error-resistant code, making it an essential concept for every Java programmer to grasp and apply in their programming practices.

Did you find this article valuable?

Support Christian Lehnert by becoming a sponsor. Any amount is appreciated!