DEV Community

Preethi Nandhagopal
Preethi Nandhagopal

Posted on

Void and Return (keywords in java)

Void-> void is a keyword that means "no return type".When the method is declared with void, it means that the method does not return any value after execution.
Used in method declaration to specify that the method return nothing.
Methods with void perform an action (like printing or modifying data) instead of giving back a value.
If a method has void,you cannot use return somevalue; , but you still use return; alone to exit early.

Return->return is a keyword used inside methods to:
Exit from a method.
Optionally send a value back to the caller (if the method has a return type other then void).

1)Returning a value->If a method has a return type (like int,String,double), it must return a value that type using return.
2)Exiting early from a method->if a method is declared as void (no return type), you can still use return; (without a value) to exit the method early.

In simple:
void->means a method returns nothing.
return->is used to send a value back or exit the method.

Top comments (0)