Functions revisited

Unlike object-oriented programs, those written in a functional way don't hold any mutable state. Instead, code is made up with functions that take arguments and return values. Because there is no internal state nor side-effects involved that can alter the execution, all functions are deterministic. This is a really good feature because it implies that different executions of the same function, with the same parameters, would produce the same outcome.

The following snippet illustrates a function that does not mutate any internal state:

public Integer add(Integer a, Integer b) {  return a + b;}

The following is the same function written using Java's functional API:

public final BinaryOperator<Integer> add = new BinaryOperator<Integer>() ...

Get Test-Driven Java Development - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.