Lazy Evaluations

In the previous section we delayed creation of heavyweight objects to make code execution faster. We’ll explore that further in this section to delay running methods, and use that approach to improve our designs. The main objective is to reduce the execution of code to the bare minimum—especially the expensive code—and speed up the execution.

Java already uses lazy execution when evaluating logical operations. For example, in fn1() || fn2(), the call fn2 is never performed if fn1 returns a boolean true. Likewise, if we replace the || with &&, the call to fn2 never happens if fn1 returns a boolean false. Programs benefit from this short-circuiting; we avoid unnecessary evaluation of expressions or functions, and that can help ...

Get Functional Programming in Java 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.