Java的突破和回访 [英] Java breaking vs returning
问题描述
我使用的返回true一个布尔值。当这样的事情发生了:
I am using a boolean that returns true. When something like this happens:
public boolean t(String label, String[] arguments) {
boolean j = false;
if (!j) {
return true;
}
return true;
}
不过,这个布尔里面我有一个开关
语句。正如你可能知道,你打破了通常开关语句。不过我做这样的事情测试布尔值是真还是假。我将返回一个switch语句内,或打破,如果一个布尔是假的?
However, inside of this boolean I have a switch
statement. As you may know, you break out of switch statements usually. However I am doing something like this testing for booleans to be true or false. Would I return inside of a switch statement, or break if a boolean is false?
一个例子是这样的:
switch (arguments.length) {
case 0:
if (j) return true;
break;
default:
break;
}
这将是更为有用,交换回如此休息,或保持不变?
Which would be more useful, exchanging return true for break, or keep it the same?
推荐答案
A 破
语句将终止开关的执行
语句,并继续在法。 A 收益
语句将离开完全的方法。
A break
statement would terminate execution of the switch
statement and continue in the method. A return
statement would leave the method entirely.
这是preference的事,真的,至于哪一个更好。归结到一点,方法设计。
It's a matter of preference, really, as to which one is better. It comes down to method design.
我会在大多数情况下,说喜欢你有以上,倒不如直接使用收益
语句,并完全退出的方法。
I would say in most cases like you have above, it would be better to directly use a return
statement and leave the method entirely.
这是说,这样的:
public boolean t(String label, String[] arguments) {
boolean j = false;
if (!j) {
return true;
}
return true;
}
可以简化为这样:
Can be simplified to this:
public boolean t(String label, String[] arguments) {
return true;
}
如果你只是从一个变量检查一个条件,那么你并不需要一个开关
语句;它不是最优的。
我做这样的测试东西布尔值是true或false
I am doing something like this testing for booleans to be true or false
您应该不是真正的测试布尔变量是真或假,只是返回它们。
You shouldn't really "test" for boolean variables to be true or false, just return them.
这篇关于Java的突破和回访的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!