In Java, void
is a keyword used to specify that a method does not return any value. When a method is declared with void
as its return type, it means that the method does not return any value to the caller. Instead, the method performs some action or task and then terminates.
For example, consider the following method:
public void printMessage(String message) {
System.out.println(message);
}
This method takes a String
parameter called message
and prints it to the console. Since the method does not return any value, it is declared with a return type of void
.
On the other hand, if a method is declared with a return type other than void
, it means that the method returns a value of that type to the caller. For example:
public int addNumbers(int a, int b) {
return a + b;
}
This method takes two int
parameters called a
and b
, adds them together, and returns the result as an int
value.
In summary, void
is used in Java to indicate that a method does not return any value to the caller.