Quick revision
The short answer
A pure function always returns the same output for the same inputs and has no observable side effects. Math.max is pure. A method that reads LocalDate.now() is not, because its result depends on the clock, and a method that adds to a list parameter is not, because it changes state the caller can see. String.toUpperCase() is pure in practice, but the no-argument form depends on the default locale, so we pass Locale.ROOT to make it consistent everywhere.
What to remember
In this answer, we saw that a pure function is deterministic and has no observable side effects. Math.max is pure; a method reading LocalDate.now() is not, because the clock is a hidden input, and the fix is to pass the date or a Clock; a method that adds to a list parameter is not, because it changes the caller's state, and the fix is to return a new list; and toUpperCase() is pure in practice but depends on the default locale, so we pass Locale.ROOT. Keeping calculations pure and pushing time, I/O and mutation to the edges makes code easier to test and reason about.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A pure function always returns the same output for the same inputs and has no observable side effects. Math.max is pure. A method that reads LocalDate.now() is not, because its result depends on the clock, and a method that adds to a list parameter is not, because it changes state the caller can see. String.toUpperCase() is pure in practice, but the no-argument form depends on the default locale, so we pass Locale.ROOT to make it consistent everywhere.
Why this question is asked
Pure functions are the most practical idea functional programming has given everyday Java. Code built from them is easier to test, safe to run on many threads and easy to cache, while impure code hides dependencies that cause flaky tests and bugs that appear only on certain machines or dates. Interviewers ask this question with concrete examples because the borderline cases, like the clock and the locale, show whether we really understand the definition. In this answer, we will define a pure function, go through the four examples one by one with the fix for each impure one, and see why the distinction matters in real code.
1What Makes a Function Pure
A function is pure when two conditions hold. First, it is deterministic: given the same arguments, it always returns the same result, so its output depends on nothing except its inputs. Second, it has no observable side effects: it does not change anything outside itself that someone else could notice, such as a field, a parameter's contents, a file, a database or the console.
A useful test is substitution. If we can replace a call such as max(3, 7) with its result, 7, everywhere in the program without changing behaviour, the function is pure. This property is called referential transparency. Notice the word observable: a function may create local objects or use a local loop counter, because nobody outside can see them.
| Function | Pure? | Why |
|---|---|---|
Math.max(a, b) | Yes | Result depends only on a and b; changes nothing |
Method reading LocalDate.now() | No | Result depends on the system clock |
| Method adding to a list parameter | No | Changes a list the caller can see |
s.toUpperCase() | In practice | Depends on the default locale; use Locale.ROOT |
2Math.max: Pure
Math.max is the textbook example. It reads only its arguments and returns one of them.
Nothing about the program, the machine or the time of day changes the answer, and calling it changes nothing. We could cache the result or replace the call with 7, and no one could tell.
3A Method Reading LocalDate.now(): Not Pure
Here is a typical business method that hides a dependency on the clock.
The first version gives different results for the same argument on different days, so it is not deterministic. That is the source of tests that pass all month and fail at midnight on the last day. The fix is to make time an input: pass today's date as a parameter, or inject a java.time.Clock and call LocalDate.now(clock), so tests can use Clock.fixed. Reading the clock does not disappear; it moves to the caller, where it is one visible line instead of a hidden dependency.
4A Method That Adds to a List Parameter: Not Pure
This one is deterministic in a sense, but it changes something the caller owns.
The impure version surprises the caller, gives different results when called twice, and even fails depending on which kind of list it receives. The pure version builds a new list and leaves the argument alone. Creating a new object is not a side effect, because nothing that existed before the call has changed.
5String.toUpperCase(): Pure in Practice, With a Catch
Strings are immutable, so toUpperCase never modifies the original; it returns a new string. That makes it look pure. The catch is that the no-argument version uses the JVM's default locale, which is part of the environment, not an argument.
In Turkish, the uppercase form of i is a dotted capital I, so code that uppercases keys, enum names or protocol words with the no-argument method can break on a server configured with a Turkish locale. Within one JVM whose default locale never changes, the method behaves purely, which is why we call it pure in practice. Passing Locale.ROOT makes the result depend only on the string, so it is pure everywhere.
6Why Purity Matters in Real Code
Pure functions are easy to test, because a test only needs inputs and an expected output, with no mocks, fixed clocks or database setup. They are safe on many threads, because they share no mutable state. Their results can be cached or computed lazily. And they are easy to reason about, because everything that affects the result is in the parameter list.
Real programs still need the clock, the database and the network. The practical goal is not to make everything pure, but to keep calculations pure and push the impure parts, reading the time, loading data, saving results, to the edges where they are few and visible.
7Follow-up questions
Is a method that logs a message pure?
Strictly no, because writing a log line is an observable side effect. In practice many teams treat logging as harmless for reasoning purposes, but a function whose behaviour depends on logging, or a test that asserts on log output, is a sign that something impure is doing real work.
Can a pure function throw an exception?
Yes, as long as it throws deterministically for the same inputs, such as Integer.parseInt("abc") always throwing NumberFormatException. The exception is then just another possible result. A function that throws only sometimes for the same input, because of a network failure for example, is not pure.
Is a method that uses Random pure?
Not if it creates or uses a shared random generator, because its result changes on every call. If the random generator, or a seed, is passed in as a parameter, the result becomes reproducible for the same seed, which is the usual way to make such code testable.
8Conclusion
In this answer, we saw that a pure function is deterministic and has no observable side effects. Math.max is pure; a method reading LocalDate.now() is not, because the clock is a hidden input, and the fix is to pass the date or a Clock; a method that adds to a list parameter is not, because it changes the caller's state, and the fix is to return a new list; and toUpperCase() is pure in practice but depends on the default locale, so we pass Locale.ROOT. Keeping calculations pure and pushing time, I/O and mutation to the edges makes code easier to test and reason about.
Open with
A pure function is one that always gives the same output for the same inputs and has no observable side effects, so nothing outside it changes. A quick test is whether I could replace a call with its result everywhere without changing the program.
Then, if they want more
Math.max is pure: it only reads its two arguments. A method that reads LocalDate.now isn't, because the clock is a hidden input; the same invoice can be not overdue today and overdue next week. I'd fix that by passing the date in, or injecting a Clock and calling LocalDate.now(clock), so tests can use a fixed clock.
A method that adds to a list parameter isn't pure either, because it changes the caller's list; calling it twice adds the element twice, and it even throws if someone passes an immutable List.of. The pure version copies the list, adds to the copy and returns it. toUpperCase is the subtle one.
Strings are immutable and it returns a new string, so it looks pure, but the no-argument form uses the default locale, and in Turkish a lowercase i uppercases to a dotted capital I, which breaks things like enum lookups on a server with that locale. Passing Locale.ROOT makes it depend only on the string.
The reason I care is practical: pure code is trivial to test, thread-safe and cacheable.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.util.stream, package documentation, java.util.function, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is a pure function? Which of these are pure and why: Math.max, a method that reads...": https://scrivio-interview-library.netlify.app/java/functional-design/093f6be2a4/