public void warning(){
System.out.println("Watch out!");
}
public void warning(){
System.out.println("Watch out!");
return;
}
We must use “return” statement with void methods. Bot of these bring same output. What’s the difference?
You needn’t use return
with void methods in Java, although some coding conventions may call for it, it is not a requirement of the language. Semantically they behave exactly the same way, as the “end” of a void method is an implicit return.
Now, it is also possible to put a return;
in a conditional block (if
, for
, while
, etc…) where it might exit the method early. For example:
void someMethod() {
if (this.avoidSomething) {
return;
}
System.out.println("Something not avoided.");
}