java'assert'和'if(){}elseexit;'的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10618582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between java ' assert 'and ' if () {} else exit;'
提问by jcy
what is the difference between java assert
and if () {} else exit;
?
javaassert
和 和有 if () {} else exit;
什么不一样?
can i just use if () {} else exit
instead of assert
?
我可以用if () {} else exit
代替assert
吗?
回答by Depado
A bit of google maybe ?
也许有点谷歌?
" The main thing you should keep in mind is that the if-else statement should be used for program flow control and the assert keyword should only be used for testing purposes. You should never use asserts to actually perform any operation required for your application to work properly. According to Sun's official Java documentation: "Each assertion contains a boolean expression that you believe will be true when the assertion executes." "
" 您应该记住的主要事情是 if-else 语句应该用于程序流控制,而 assert 关键字应该仅用于测试目的。您永远不应该使用断言来实际执行应用程序所需的任何操作正常工作。根据 Sun 的官方 Java 文档:“每个断言都包含一个布尔表达式,您认为该表达式在断言执行时为真。”“
阅读更多:http: //wiki.answers.com/Q/What_is_the_difference_between_assert_keyword_and_if_keyword_in_java#ixzz1v2GGfAhq
回答by Jigar Joshi
you could, assert is specifically designed to assert some part of code,
你可以,断言专门用于断言代码的某些部分,
assert will throw AssertionError
if it fails to assert
AssertionError
如果断言失败,断言将抛出
Also See
另见
回答by Tiago Peczenyj
I can just ignore the assertion
我可以忽略断言
class A{
public static void main(String[] args) {
assert false;
System.out.println("hi");
}
}
This code will print hi by default
默认情况下,此代码将打印 hi
$ java -cp . A
hi
$ java -ea -cp . A
Exception in thread "main" java.lang.AssertionError
at A.main(A.java:6)
回答by Kai
if-else is for controlling the flow of your program. assert must not be used for this! Asserts are just for having "checkpoints" in your code to check conditions which "should always" match.
if-else 用于控制程序的流程。断言不能用于此!断言只是为了在你的代码中有“检查点”来检查“应该总是”匹配的条件。
Asserts can be turned on or off when running a program so that you could enable the asserts on your development machine/integration environment and disable them in the released version.
运行程序时可以打开或关闭断言,以便您可以在开发机器/集成环境中启用断言并在发布版本中禁用它们。
回答by Wim Deblauwe
The assert keyword will only trigger an exception when you enable the assertions with -ea
on the command line. The if/else will always execute. On top of that the assert keyword allows to write less verbose code. Compare:
assert 关键字只会在您-ea
在命令行上启用断言时触发异常。if/else 将始终执行。最重要的是,assert 关键字允许编写较少冗长的代码。比较:
assert parameter != null;
to:
到:
if( parameter != null )
{
}
else
{
}
回答by Stephen C
can i just use if () {} else exit instead of assert ?
我可以只使用 if () {} else exit 而不是 assert 吗?
Yes you could, but ...
是的,你可以,但是...
you wouldn't be able to turn of the test unless you made the condition depend on an extra flag (which makes it more complicated, etc),
you wouldn't get a chance to perform tidy-up in any enclosing
finally
block,it would be more complicated to log the cause of the problem than if an
AssertionError
(or any other exception) was thrown, andif your code needed to be reused (as-is) in another application, your calls to
exit
could be problematic.
除非您使条件依赖于一个额外的标志(这使它变得更加复杂等),否则您将无法关闭测试,
你不会有机会在任何封闭
finally
块中进行整理,记录问题的原因会比
AssertionError
抛出(或任何其他异常)更复杂,并且如果您的代码需要在另一个应用程序中(按原样)重用,则您的调用
exit
可能会出现问题。
回答by Pablo
Asserts are ignored unless the -ea param is passed:
除非传递 -ea 参数,否则断言将被忽略:
java -ea myjar.jar
java -ea myjar.jar
That way, you can use them when you are testing your application, but ignore them at other times.
这样,您可以在测试应用程序时使用它们,但在其他时候忽略它们。