An assertion (assertion) is a statement in Java that contains a Boolean expression that, when the Boolean value is True, the program is considered correct, and when the Boolean value is False, the system throws an error.
Assertions are disabled by default and can be turned on at development time, helping to correct errors and increase maintainability.
PS: Assertion, in other words is standing flag,false is a slap and slap face.
Assert two statements in the form of
assert Expression1; assert Expression1:expression2;
Expression1 is a Boolean expression that is true or false as an assertion.
Expression2 is an expression with a return value that is used to provide detailed error information.
When there is no Expression2, the default is to throw Assertionerror with no detailed information.
Using the assertion Scenario
1, the variable value is clear
The value of a variable can be asserted if it is determined to be a value.
2. Statements that cannot be executed
A local statement determines that execution is not achieved, such as a switch with no default, and the default can be asserted.
3. Pre-conditions
A precondition (precondition) is a statement that must be true before the method executes.
Example: Judging a thread's own lock, combined with assertions, can test if the front thread has a lock on an object.
Privateobject[] A; Public synchronized intfind (Object key) {returnFind (Key, a, 0, a.length);}//Recursive Helper method-always called with a lock in this.Private intFind (Object key, object[] arr,intStartintLen) { assertThread.holdslock ( This);//Lock-status Assertion ...}
I think the exception to the preconditions is not to use assertions as good, either public or private.
4. Post conditions
A post condition (postcondition) is a statement that is true after the current condition is satisfied and the method is fully executed.
/*** Returns A BigInteger whose value is (this-1 mod m). * * @paramm the modulus. * @returnthis-1 mod M. * @throwsarithmeticexception m <= 0, or this BigInteger *has no multiplicative inverse mod m (that's, this BigInteger * Is isn't relatively prime to m). */ PublicBigInteger Modinverse (BigInteger m) {if(M.signum <= 0) Throw NewArithmeticException ("Modulus not positive:" +m); ... //Do the computation assert This. Multiply (Result). mod (M). Equals (one): This; returnresult;}
View Code
5, check the status of the class
Adds an internal method that returns a Boolean value that returns True when the class state is checked.
Do not use assertion scenarios
1, do not use assertions to do parameter checking
Because the argument must be checked regardless of whether the assertion is enabled or disabled, the assertion may be disabled. and asserts that the assertionerror exception is reported, and cannot accurately feed back runtime exceptions (such as Illegalargumentexception,indexoutofboundsexception or NullPointerException).
2. Do not use assertions to complete any work required for the correct operation of the program
For example, suppose you want to remove all empty elements from the list name and know that the list contains one or more null values.
The wrong approach:
// behaviors are included in assertions assert Names.remove (null);
When an assertion is enabled, the program works correctly, but fails when disabled because it no longer removes empty elements from the list.
The correct usage:
// fixed-action precedes assertion boolean nullsremoved = Names.remove (null); assert // run whether assert is enabled
Open assertion
You can add-ea to the VM arguments in the Eclipse run configuration.
Reference documents
Https://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html
Java Assertion (assertion)