Creating a Supplier does not run its body; the lambda's work only happens the moment get() is invoked, which is what makes Supplier useful for lazy or repeatable computation.
System.out.println("Before creating supplier");
Supplier<String> supplier = () -> {
System.out.println("Computing now");
return "result";
};
System.out.println("Supplier created, not yet computed");
System.out.println("Value: " + supplier.get());
Before creating supplier
Supplier created, not yet computed
Computing now
Value: result
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-09-27