Java - What is the output: jump out of the lambda expression?

Question

What is the output of the following code?

import java.util.function.Consumer;

public class Main {
  public static void main(String[] args) {
    for (int i = 0; i < 5; i++) {
      Consumer<Integer> evenIdPrinter = id -> {
        if (id < 0) {
          break;
        }
      };
    }
  }
}


Click to view the answer

if (id < 0) {
   // A compile-time error
   // Attempting to break out of the lambda body
   break;
}

Note

In the code, the break statement is inside a for-loop statement and the body of a lambda statement.

If this break statement is allowed, it will jump out of the body of the lambda expression.

This is the reason that the code generates a compile-time error.