Lesson 13Lambda Expressions and Functional Style Programming
Presenting materials of this chapter is somewhat challenging. From the beginning of this book you’ve gotten to know that Java is an object-oriented language (as opposed to a functional language). You’ve learned that to start any program you’ll need to define classes with attributes and methods, instantiate them as objects, and invoke methods (think functions) on these instances. If a method is declared as static
, you can invoke it without the need to instantiate a class, but still you were defining the class first and then a static method inside of it. For example, to process a click on a Swing JButton
, you would need to implement and instantiate a listener class containing just one method actionPerformed()
.
Now I need to tell you that the object-oriented approach may have some drawbacks, and there is a large group of functional programming languages in which you don’t need to wrap functionality inside classes. Proponents of functional languages say (and rightly so) that creating objects with fields that can be changed (mutated) by the methods’ code may be error prone and more difficult to debug. They prefer thinking in terms of functions that don’t depend on the external context.
A function takes the values from the outside world in the form of arguments, apply some application logic and returns the result. A function doesn’t change any external values (including those that were provided as arguments). A function ...
Get Java Programming 24-Hour Trainer, 2nd 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.