Java lambda with no arguments and no return value (void and Void)
Codemia Knowledge Hub ·
A lambda that takes no arguments and returns nothing is written () -> { ... } and implements a functional interface whose single method returns void. The standard example is Runnable:
The capital-V Void class is a different thing. It is an uninstantiable placeholder that holds the Class<Void> object for the keyword void. You only need it in generic code, where type arguments must be real classes or interfaces and the primitive keyword void cannot be used.
Lambda syntax
Lambda expressions were one of the biggest additions in Java 8 and brought a more functional style to the language. A lambda is an anonymous method (a function without a name) that you mainly pass as a block of behaviour to other methods. Lambdas simplify code that works with collections and with APIs that expect an implementation of a single abstract method. They come in two forms:
Running a no-argument lambda on a thread
Thread accepts a Runnable, so you can pass the lambda directly:
The lambda passed to the Thread constructor returns nothing. Its block of statements simply runs on the new thread. In the first Runnable example, likewise, the lambda takes no parameters, returns no value and just executes the print statement.
Where no-result lambdas are used
These lambdas fit anywhere an action is performed without directly manipulating or returning data. They are especially handy when designing APIs that run actions when an event occurs, process collections or handle asynchronous operations:
- Event listeners: setting up UI events in frameworks.
- API callbacks: running code after an asynchronous operation completes.
- Thread operations: running code on a separate thread.
- Logging: performing a side effect without producing a value.
| Aspect | Description |
| Lambda Expression | A compact way to represent an anonymous method that can be used to implement functional interfaces. |
| No-result lambdas | Useful in contexts where no return value is needed, like event handling or threading. |
| Common Interfaces | Runnable, generally used for threads, is the typical functional interface for a lambda with no arguments and no result. |
| Benefits | Reduces the boilerplate code seen in older Java versions, making the code more readable and maintainable. |
Related reading
- Java 8 lambdas, Function.identity() or t->t
- Java 8 List<V> into Map<K, V>
- Java 8 LocalDate Jackson format
- Java 8 method references provide a Supplier capable of supplying a parameterized result
- Java 8 performance of Streams vs Collections
- Java 8 Stream and operation on arrays
- Java 8 Streams - collect vs reduce
- Java 8 Streams multiple filters vs. complex condition
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.