Java中的assert语句是什么?
可能的重复:
断言有什么作用?
请给我一些详细信息,并至少提供一个示例。
Possible Duplicate:
What does assert do?
Please give me some details with at least one example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
如果您编译并运行它,您应该了解断言语句是如何工作的。
更新:
正如注释中正确指出的那样,编译后,您将其运行为
java -ea AssertionTest
--ea
标志启用断言。Try this:
If you compile and run this, you should have an idea of how the assertion statement works.
Update:
As correctly pointed out in the comments, after compilation, you run this as
java -ea AssertionTest
- the-ea
flag enables assertions.您可以使用
assert
关键字来验证您对代码的看法是否正确。中的断言不能替代代码验证,因为它可以在运行时禁用(默认情况下禁用)因此,如果断言被禁用并且您使用它来控制逻辑,您将产生不良结果。
例如:
在该代码中,程序对输入执行某些操作。您的假设是,结果字符串始终以
"OK"
开头。你放置断言,以确保发生这种情况,但你没有在周围放置任何逻辑(如果没有发生,你不会做任何事情)当你测试你的代码时,启用断言,如果你注意到你的结果不是以“OK”开头,那么程序将停止执行。
要启用/禁用断言,您必须将标志
-ea
传递给 java请参阅:http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html 了解更多信息。
You use the
assert
keyword to verify if something you believe about your code is true.The assertion in not a substitute for code validations, because it can be disabled at runtime ( it is disabled by default ) So, if the assertion is disabled and you use it to control your logic, you'll have undesired results.
For instance:
In that code, the program does something with the input. Your assumption is, that the result string starts with
"OK"
always. You put the assertion, to make sure that happens, but you don't put any logic around that ( you don't do anything if that doesn't happens )When you're testing your code, with assertion enabled, if you notice your result doesn't start with "OK", then the program will stop it's execution.
To enable/disble assertions, you have to pass the flag
-ea
to the javaSee : http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html for more information.
断言是 Java™ 编程语言中的一条语句,使您能够测试对程序的假设。例如,如果您编写一个计算粒子速度的方法,您可能会断言计算出的速度小于光速。
每个断言都包含一个布尔表达式,您认为该断言执行时该表达式将为 true。如果不正确,系统将抛出错误。通过验证布尔表达式确实为真,断言证实了您对程序行为的假设,增加了您对程序没有错误的信心。
查看下面的链接以获取更多详细信息和示例 -
http: //download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html
http://www.roseindia.net/javacertification/scjp5/assertionsexample.shtml
An assertion is a statement in the JavaTM programming language that enables you to test your assumptions about your program. For example, if you write a method that calculates the speed of a particle, you might assert that the calculated speed is less than the speed of light.
Each assertion contains a boolean expression that you believe will be true when the assertion executes. If it is not true, the system will throw an error. By verifying that the boolean expression is indeed true, the assertion confirms your assumptions about the behavior of your program, increasing your confidence that the program is free of errors.
Check out links below for more details and examples -
http://download.oracle.com/javase/1.4.2/docs/guide/lang/assert.html
http://www.roseindia.net/javacertification/scjp5/assertionsexample.shtml