问题
This question already has an answer here:
- What does the Java assert keyword do, and when should it be used? 18 answers
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);
}
回答1:
If you launch your program with -enableassertions
(or -ea
for short) then this statement
assert cond;
is equivalent to
if (!cond)
throw new AssertionError();
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 (!(d >= 0 && d <= s.length()))
throw new AssertionError();
(If you launched with -enableassertions
that is.)
Formally, the Java Language Specification: 14.10. The assert Statement says the following:
14.10. The
assert
Statement
An assertion is anassert
statement containing a boolean expression. An assertion is either enabled or disabled. If the assertion is enabled, execution of the assertion causes evaluation of the boolean expression and an error is reported if the expression evaluates tofalse
. If the assertion is disabled, execution of the assertion has no effect whatsoever.
Where "enabled or disabled" is controlled with the -ea
switch and "An error is reported" means that an AssertionError
is thrown.
And finally, a lesser known feature of assert
:
You can append : "Error message"
like this:
assert d != null : "d is null";
to specify what the error message of the thrown AssertionError should be.
This post has been rewritten as an article here.
回答2:
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
回答3:
assert
is a debugging tool that will cause the program to throw an AssertionFailed
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
回答4:
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:
String strA = null;
String strB = null;
if (2 > 1){
strA = "Hello World";
}
strB = strA.toLowerCase();
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:
assert strA != null; //Adding here
strB = strA .toLowerCase();
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.
回答5:
Use this version of the assert statement to provide a detail message for the AssertionError. The system passes the value of Expression2 to the appropriate AssertionError constructor, which uses the string representation of the value as the error's detail message.
The purpose of the detail message is to capture and communicate the details of the assertion failure. The message should allow you to diagnose and ultimately fix the error that led the assertion to fail. Note that the detail message is not a user-level error message, so it is generally unnecessary to make these messages understandable in isolation, or to internationalize them. The detail message is meant to be interpreted in the context of a full stack trace, in conjunction with the source code containing the failed assertion.
JavaDoc
回答6:
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.
回答7:
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
回答8:
Assert does throw an AssertionError if you run your app with assertions turned on.
int a = 42;
assert a >= 0 && d <= 10;
If you run this with, say: java -ea -jar peiska.jar
It shall throw an java.lang.AssertionError
来源:https://stackoverflow.com/questions/3018683/what-does-the-assert-keyword-do