“断言”是什么?关键字做什么?
assert
的作用是什么? 例如在函数中:
private static int charAt(String s, int d) {
assert d >= 0 && d <= s.length();
if (d == s.length()) return -1;
return s.charAt(d);
}
What does assert
do?
For example in the function:
private static int charAt(String s, int d) {
assert d >= 0 && d <= s.length();
if (d == s.length()) return -1;
return s.charAt(d);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您使用
-enableassertions
(或简称-ea
)启动程序,那么此语句相当于
如果您在没有此选项的情况下启动程序,则断言语句将没有影响。
例如,断言 d >= 0 && d <= s.length();,正如您的问题中所发布的,相当于
(如果您使用
-enableassertions
启动。)正式地, Java 语言规范:14.10。
assert
语句 的内容如下:其中“启用或禁用”由
-ea
开关和控制“报告错误”意味着抛出AssertionError
。最后,
assert
的一个鲜为人知的功能:您可以像这样附加
: "Error message"
:指定抛出的 AssertionError 的错误消息应该是什么。
If you launch your program with
-enableassertions
(or-ea
for short) then this statementis equivalent to
If you launch your program without this option, the assert statement will have no effect.
For example,
assert d >= 0 && d <= s.length();
, as posted in your question, is equivalent to(If you launched with
-enableassertions
that is.)Formally, the Java Language Specification: 14.10. The
assert
Statement says the following:Where "enabled or disabled" is controlled with the
-ea
switch and "An error is reported" means that anAssertionError
is thrown.And finally, a lesser known feature of
assert
:You can append
: "Error message"
like this:to specify what the error message of the thrown AssertionError should be.
如果不满足条件,将抛出
AssertionError
。不过,必须启用断言;否则,
assert
表达式不会执行任何操作。请参阅:http://java.sun .com/j2se/1.5.0/docs/guide/language/assert.html#enable-disable
If the condition isn't satisfied, an
AssertionError
will be thrown.Assertions have to be enabled, though; otherwise the
assert
expression does nothing. See:http://java.sun.com/j2se/1.5.0/docs/guide/language/assert.html#enable-disable
assert
是一个调试工具,如果条件不成立,它会导致程序抛出AssertionFailed
异常。在这种情况下,如果程序后面的两个条件中的任何一个计算结果为 false,则程序将引发异常。一般来说,assert
不应该在生产代码中使用assert
is a debugging tool that will cause the program to throw anAssertionFailed
exception if the condition is not true. In this case, the program will throw an exception if either of the two conditions following it evaluate to false.Generally speaking,assert
should not be used in production code尽管我已经阅读了很多有关此的文档,但我仍然对如何、何时以及在何处使用它感到困惑。
使其非常容易理解:
当您遇到类似的情况时,如下所示:
您可能会收到警告(在 strB = strA.toLowerCase(); 上显示黄线),strA 可能会向 strB 生成 NULL 值。虽然你知道 strB 最终绝对不会为 null,但为了以防万一,你对
1 使用了断言。禁用警告。
2.如果发生最坏的情况(当您运行应用程序时),则抛出异常错误。
有时,当您编译代码时,您不会得到结果,这是一个错误。但应用程序不会崩溃,并且您会花费非常困难的时间来查找导致此错误的位置。
因此,如果你像这样放置断言:
你告诉编译器 strA 绝对不是空值,它就可以“和平地”关闭警告。如果它是 NULL(最坏的情况发生),它将停止应用程序并向您抛出一个错误来定位它。
Although I have read a lot documentation about this one, I'm still confusing on how, when, and where to use it.
Make it very simple to understand:
When you have a similar situation like this:
You might receive warning (displaying yellow line on strB = strA.toLowerCase(); ) that strA might produce a NULL value to strB. Although you know that strB is absolutely won't be null in the end, just in case, you use assert to
1. Disable the warning.
2. Throw Exception error IF worst thing happens (when you run your application).
Sometime, when you compile your code, you don't get your result and it's a bug. But the application won't crash, and you spend a very hard time to find where is causing this bug.
So, if you put assert, like this:
you tell the compiler that strA is absolutely not a null value, it can 'peacefully' turn off the warning. IF it is NULL (worst case happens), it will stop the application and throw a bug to you to locate it.
JavaDoc
JavaDoc
断言通常主要用作检查程序预期行为的一种手段。在大多数情况下,它应该会导致崩溃,因为程序员对程序状态的假设是错误的。这就是断言的调试方面的用武之地。它们创建了一个检查点,如果我们想要获得正确的行为,我们就无法忽略该检查点。
在您的情况下,它会对传入参数进行数据验证,尽管它并不能阻止客户端将来滥用该功能。特别是如果它们没有(也不应该)包含在发布版本中。
Assertions are generally used primarily as a means of checking the program's expected behavior. It should lead to a crash in most cases, since the programmer's assumptions about the state of the program are false. This is where the debugging aspect of assertions come in. They create a checkpoint that we simply can't ignore if we would like to have correct behavior.
In your case it does data validation on the incoming parameters, though it does not prevent clients from misusing the function in the future. Especially if they are not, (and should not) be included in release builds.
它确保表达式返回 true。否则,它会抛出
java.lang.AssertionError
。http://java.sun.com/docs/ books/jls/third_edition/html/statements.html#14.10
It ensures that the expression returns true. Otherwise, it throws a
java.lang.AssertionError
.http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.10
如果您在打开断言的情况下运行应用程序,Assert 确实会引发 AssertionError。
如果您运行此命令,请说:java -ea -jar peiska.jar
它将抛出java.lang.AssertionError
Assert does throw an AssertionError if you run your app with assertions turned on.
If you run this with, say: java -ea -jar peiska.jar
It shall throw an java.lang.AssertionError