void
In short: The placeholder return type for methods that return no value — stands in place of a real type like int or String in the method header.
In more detail: A void method can still contain an empty return; (with no value), to end execution early. Unlike methods with a real return type, calling a void method can’t be used as a value in an expression.
In Depth
void greet(String name) {
if (name == null || name.isEmpty()) {
System.out.println("No name given");
return; // ends the method early, WITH NO value returned
}
System.out.println("Hello, " + name + "!");
}
// Error: a void method can't be used as an expression
// int result = greet("Anna"); // compiler errorStrictly speaking, void isn’t a “real” data type like int or String — you can’t declare a variable of type void, and the return value of a void method can’t be used in an assignment or an expression, because there simply is no value that comes back. Nevertheless, a void method may contain an empty return; (with no following value), to end execution early, for example after an early error check like in the example above (“guard clause”) — this differs from return value; for methods with a real return type.
void is typically used for methods whose purpose is a side effect (printing something, changing something, saving something) rather than a computation whose result the caller wants to further process — the latter requires a concrete return type. The special wrapper object Void (capitalised, a genuine object) does also exist, but is used almost exclusively in generic programming, when a type parameter formally requires a type but you actually want to express “nothing”.