Return
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
.
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.
Break只能跳出一重循环(经测试验证)。
The switch statement requires a selector that evaluates to an integral value, such as int or char.
The infamous “goto”
Although
goto is a reserved word in Java, it is not used in the language; Java has no
goto. However, it does have something that looks a bit like a jump tied in with the
break and
continue keywords.
It’s not a jump but rather a way to break out of an iteration statement. The reason it’s often thrown in with discussions of goto is because it uses the same mechanism:
a label.
The only place a label is useful in Java is right before an iteration statement.And that means
right before—it does no good to put any other statement between the label and the iteration.
And the sole reason to put a label before an iteration is if you’re going to nest another iteration or a switch (which you’ll learn about shortly) inside it. That’s because the break and continue keywords will normally interrupt only the current loop
, but when used with a label, they’ll interrupt the loops up to where the label exists.(使用lable的原因是需要跳出多重循环,而break与continue只能跳出单重循环)
The same rules hold true for while:
1. A plain continue goes to the top of the innermost loop and continues.
2. A labeled continue goes to the label and reenters the loop right after that label.
3. A break “drops out of the bottom” of the loop.
4. A labeled break drops out of the bottom of the end of the loop denoted by the label.
1. A plain continue goes to the top of the innermost loop and continues.
2. A labeled continue goes to the label and reenters the loop right after that label.
3. A break “drops out of the bottom” of the loop.
4. A labeled break drops out of the bottom of the end of the loop denoted by the label.