Java 8
Lambda Expressions
Void Argument
Programming
Software Development

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:

java
Runnable runExample = () -> {
    System.out.println("This is an example of a Void lambda expression.");
};

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:

java
(parameters) -> expression
or
(parameters) -> { statements; }

Running a no-argument lambda on a thread

Thread accepts a Runnable, so you can pass the lambda directly:

java
1// Using lambda expression to create a thread 
2Thread thread = new Thread(() -> {
3    for (int i = 0; i < 5; i++) {
4        System.out.println("Child Thread value: " + i);
5    }
6});
7thread.start();

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.
AspectDescription
Lambda ExpressionA compact way to represent an anonymous method that can be used to implement functional interfaces.
No-result lambdasUseful in contexts where no return value is needed, like event handling or threading.
Common InterfacesRunnable, generally used for threads, is the typical functional interface for a lambda with no arguments and no result.
BenefitsReduces the boilerplate code seen in older Java versions, making the code more readable and maintainable.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data 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.

Practice algorithms