The labs on this site need JavaScript to run. The same content is written out below.
Fundamentals › Java
What is a lambda a shorter way to write?
A lambda is short code written with an arrow (->). () -> System.out.println("Working") is a short way to write "an object that implements an interface with only one method." Here we use a Task interface that has just one method, run, and the code after the arrow is what run does. Written out in full, the same thing is code that creates a class that implements Task and creates an object with new. In this lab you write the lambda out as an anonymous class and as a class with a name and see that the result is the same, and you compile and run the same code to see whether the code after the arrow runs on the line where the lambda is written, what happens if you remove t.run() or call it twice, and what happens if Task has one more method.
Checked against real execution · Eighteen combinations were compiled with javax.tools on JDK 21, run, and checked
interface Task {
void run();
}
public class Main {
public static void main(String[] args) {
// Lambda: stores what run does. It does not run on this line
Task t = () -> System.out.println("Working");
System.out.println("Ready");
t.run();
}
}
The compiler and runtime are JDK 21.0.12.1 (javac 21). For all eighteen combinations, the source that is the same as the code on the screen (identical text, line by line) was compiled with javax.tools, and for those that compiled, the class file was loaded with a new class loader and Main.main was run, and the results were checked against the standard output that was captured. What was checked is whether compilation succeeded, the diagnostic code of the first error (the "Task is not a functional interface, multiple non-overriding abstract methods found in interface Task" of compiler.err.prob.found.req was restated as "Task has more than one method, so it cannot be used as a lambda," and the "does not override abstract method stop() in Task" of compiler.err.does.not.override.abstract was restated as "The stop method of Task was not implemented"), the number of errors and the line where each error occurred, whether the Main class file was created, and the output when run. The facts that the step sentences rely on were checked by running separate source in the same way. Right after the line that stores the object, t is an object of a class that implements Task in all three forms, and "Working" was not printed on that line (the lambda is an object of a hidden class that is created on this JDK, and the anonymous class and the class with a name, Work, were each an object of the class with exactly that name). The documentation was checked by opening the Java Language Specification (Java SE 21) directly. That a lambda expression has a functional interface as its target type in assignment, invocation, and casting contexts is from Section 15.27.3; that evaluating a lambda expression at run time creates a reference to an object of a class that implements the target functional interface, which is different from executing the body of the lambda, and that the body of the lambda is executed when a method of that interface is invoked, is from the introduction of Section 15.27 and Section 15.27.4; that a functional interface is an interface that has just one abstract method (not counting the methods of Object), and that an object of it can also be created with a lambda expression, is from Section 9.8; and that an anonymous class is declared implicitly by a class instance creation expression and can have an interface as its direct superinterface is from Section 15.9.5. Only part of each of those sections came back as a summary, so I could not read the whole original text. The kind of error, the number of errors, and the line where it occurs are not defined by the specification; they were observed on this javac. This lab covers only "a lambda is a short way to write an object that implements an interface with only one method" and when it runs. The object that a lambda creates is not exactly the same as the object of an anonymous class. On this JDK it is created as a hidden class, and it is said to differ in other ways too, such as what this refers to, but this lab did not check that. Lambdas with parameters, lambdas that return a value, method references, built-in functional interfaces such as Runnable and Comparator, and interfaces with default methods or static methods are not covered, and they are covered in the in-depth lab "Lambda expressions and functional interfaces".
Previous lab: List and Map · Next lab: Streams
Struck-through text was removed from the first version of the code; highlighted text was added.