The return Statement in Java
return immediately exits the method it's inside, handing a value back to the caller if the method's return type isn't void, or simply ending execution if it is. Once a return statement runs, nothing else in that method's body executes — including any remaining loop iterations or statements after it — which makes return a genuine early-exit tool, not just a way to end a method at its very last line.
A non-void method must return a value on every possible path through its code, which the compiler actually verifies. A method with an if-else where only the if branch returns a value is a compile error, since the compiler can't prove the else path always returns too.
Ending a void method early
A bare return; inside a void method exits immediately, skipping anything after it.
Returning a value
return value; both exits the method and hands value back to whatever called it.
Execution stops immediately
Nothing after a return statement in the same execution path ever runs, loops included.
Every path must return
The compiler requires a non-void method to return a value on every possible branch through its logic.
return inside a loop
Exits the entire method immediately, not just the current loop iteration, which is a common point of confusion with break.
public class ReturnDemo { static String checkNumber(int n) { if (n < 0) { return "negative"; } if (n == 0) { return "zero"; } return "positive"; } public static void main(String[] args) { System.out.println(checkNumber(-5)); System.out.println(checkNumber(0)); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.