Zero Parameters: Empty Parentheses
You'll learn how to write lambda expressions that accept no input parameters using the () syntax.
The Core Idea
When a lambda expression doesn't need any input to do its work, you must still indicate this explicitly by writing empty parentheses () before the arrow. This is not optional—Java's syntax requires the parentheses even when they're empty.
Think of it like a phone conversation: when you pick up the phone to deliver a message, you still say "hello" even if you don't need any information from the other person. The empty parentheses are your lambda's way of saying "I'm here, but I don't need any inputs."
The Syntax
() -> expression
or for multiple statements:
() -> {
statements;
}
When You Need This
You'll use empty parentheses with functional interfaces that take no arguments, particularly Supplier<T>:
Supplier<String> greeting = () -> "Hello, World!";
String message = greeting.get(); // "Hello, World!"
Supplier<Double> randomValue = () -> Math.random();
double value = randomValue.get(); // e.g., 0.734521
With Runnable (which returns nothing):
Runnable task = () -> System.out.println("Task executed!");
task.run(); // Prints: Task executed!
Why Parentheses Are Required
Java requires the () to distinguish zero-parameter lambdas from single-parameter lambdas (where you can omit parentheses). Without this rule, the syntax would be ambiguous.
Key Takeaway: When your lambda expression takes no parameters, you must write empty parentheses () before the arrow to indicate zero inputs—this is mandatory syntax, not optional.