Have a following code:
static int counter = 0;
static Callable<?> callable = () -> counter;
static Runnable runnable = () -> counter++;
static Runnable runnable2 = () -> counter; // DOES NOT COMPILE
public static void main(String[] args) throws Exception {
runnable.run();
System.out.println(callable.call()); // PRINTS "1"
}
I understand why runnable2 does not compile - because lambda returns a value and runnable does not have return type (is void). I also understand why callable compile, it returns counter variable value, which was incremented by calling run method. (I know it's single threaded but that's not the point).
What I don't understand is why runnable
lambda actually compiles? As far as I know, ++ operator does return a value - incremented value - why does compiler ignore it?