return
Several keywords represent unconditional branching, which simply means that the branch happens without any test. These include return, break, continue, and a way to jump to a labeled statement which is similar to the goto in other languages.
The return keyword has two purposes: It specifies what value a method will return (if it doesn’t have a void return value) and it causes the current method to exit, returning that value. The preceding test( ) method can be rewritten to take advantage of this:
//: control/IfElse2.java import static net.mindview.util.Print.*; public class IfElse2 { static int test(int testval, int target) { if(testval > target) return +1; else if(testval < target) return -1; else return 0; // Match } public static void main(String[] args) { print(test(10, 5)); print(test(5, 10)); print(test(5, 5)); } } /* Output: 1 -1 0 *///:~
There’s no need for else, because the method will not continue after executing a return.
If you do not have a return statement in a method that returns void, there’s an implicit return at the end of that method, so it’s not always necessary to include a return statement. However, if your method states it will return anything other than void, you must ensure every code path will return a value.
[Thinking in Java, 99]