return
In short: return ends a method and optionally gives a value back to the caller.
In more detail: The return type stands before the method name. With void nothing is returned, and return; then just ends the method.
In Depth
static int square(int x) {
return x * x; // returns the value
}
int y = square(5); // y = 25Notes
- After
return, no further code of the method is executed. - All paths of a method with a return type must return a value.
- An early
return(guard clause) makes code more readable because nested if blocks disappear.