Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free
lesson

Pure Functions

Pure Functions

In this lesson — part of Functional Foundations — you'll learn pure functions in Java and why it matters in real work.

Why it matters

Functional style — pure functions and immutability — makes code easier to test and reason about.

Key ideas

  • Pure functions
  • Immutability
  • No shared mutable state
  • Predictable outputs

In practice

Here's how it looks in idiomatic Java:

// Pure: output depends only on inputs, no side effects, no mutation
public static int square(int x) {
    return x * x;
}

public static void main(String[] args) {
    System.out.println(square(5)); // always 25, no external state touched
}

Java note: Java has no pure keyword; purity is a discipline — for a function to be pure it must avoid I/O, mutation of shared state, and reliance on anything but its arguments.

Try it yourself

Exercise: In Java, rewrite a function that mutates a global so it's pure.

Recap

You now understand pure functions and can apply it in Java. Mark this lesson complete and continue to the next one.