The return statement in Java is always closely related to the method, which is always used in the method, has two functions, one is the value that returns the specified type of the method (this value is always determined), and one is the execution of the End method (only a return statement).
| public void Test1 () {System.out.println ("---------no return Statement Test--------"); for (int i = 1;; i++) {if (i = = 4) return; System.out.println ("i =" + i); } } |
When I=4, the loop ends.
Return has a feature that ends the method directly as soon as the return is invoked.
There are the following examples:?
| public int Locate () {while (true) return 4; } |
The above code is not an error,
?
| public int Locate () {while (IsTrue ()) return 4; Boolean isTrue () {return true; } |
This code has an error, because the compiler believes that the while statement has the ability to execute in any case, but only if the argument is true. The JVM doesn't know what to do when istrue is fake, so it complains.
Looking at the following code:
?
| public int Locate () {while (IsTrue ()) return 4; return 0; Boolean isTrue () {return true; } |
This code also does not complain because it added return 0; Although the JVM does not know what to do when IsTrue is false, it still returns a value of 0, and the method returns a value.