Understanding functions in Java
In Java, we call units of code in a class a method. In C and C++, we call them functions. In JavaScript, we even use the keyword
function. What sets Java apart from these other languages is that functions represent a different coding model than classes and their methods. There are functional rather than object-oriented (OO) languages, of which Haskell is one example. We are briefly examining functions because our next topic, streams, is based on the function rather than the class model.
Let’s look at some code that attached an event handler to a button in JavaFX. We will be looking at JavaFX in Chapter 13, Desktop Graphical User Interface Coding with Swing and JavaFX. Let’s begin by looking at what a functional EventHandler
interface is:
@FunctionalInterface public interface EventHandler<T extends Event> extends EventListener { void handle(T event); }
This is the interface class...