void
In short: A special return type for methods that return no value, but only perform an action (e.g. printing something or changing a state).
In more detail: A method declared void may be ended early with return; (with no value), but not return a concrete value — so the caller can’t store the result in a variable. The opposite are methods with a concrete return type like double or a reference type, which always have to return a matching value.
In Depth
void printGreeting(String name) {
System.out.println("Hello, " + name + "!");
// no "return value;" possible - only "return;" (optional, early end)
}
double calculateArea(double width, double height) {
return width * height; // MUST return a double
}The difference shows up directly at the call site: the result of a void method can’t meaningfully be assigned to a variable (x = printGreeting("Alex") would be a compile error in most languages), while a method with a return type is meant for exactly that. void thus clearly signals: “this method is called for its SIDE EFFECTS (printing something, changing a state), not for a result.”
return; (with no value) is still allowed and useful in a void method, to end execution EARLY, for example after an early condition check:
void processInput(String input) {
if (input == null || input.isEmpty()) {
return; // nothing more to do, exit the method immediately
}
// ... rest of the processing
}The special case void main(...) — the classic program entry point of many languages — is void precisely because a program returns no value to “the caller” in the usual sense when it terminates; instead, programs usually communicate their success/failure via a separate mechanism (the operating system process’s exit code), not via the normal return-value mechanism of an ordinary method.