Code is always data! The bytecode instructions for the program are stored in memory. The programmer does not have direct access to read and write to this memory, but there.
When we talk about “processing” code as data, we refer to storing references to this memory so that they can be used and organized in the way we usually think of “data”.
For example, you can imagine that inside the button it looks something like this:
class Button { private ActionListener[] listeners = new ActionListener[100]; private int count = 0; public void addActionListener(ActionListener listener) { listeners[count] = listener; ++count; }
Basically, a button stores a list of functions in an array: it is processing the code as data. In most cases, an ActionListener serves no other purpose than to refer to a specific actionPerformed override.
Since all Java instance methods are virtual, we can always treat code as data. This is demonstrated by AWT / Swing, designed for listening. Lambda expressions simply add a conceptual accent and short syntax. (Also an increase in performance in most situations due to their implementation.)
Lambdas allows us to more clearly express our intent when we use an object only to implement a specific method, and not to store values.
From a technical point of view, all this is done indirectly. ActionListener does not contain code: instead, the virtual machine somehow knows (that we do not know) that it points to a specific structure in memory that contains a pointer to the code. Thus, some reference to the code is what is actually transmitted, therefore, "processing the code as data."
source share