Java assert and javaassert
Assertions
In Java, similar to C and C ++, The assert keyword is used to indicate assertions. The usage and meaning are similar.
Usage
Assert <boolean expression>
If it is false, the program throws an AssertionError and terminates the execution.
Assert <boolean expression >:< error message expression>
If <boolean expression> is true, the program continues to execute.
If it is false, the program throws a java. lang. AssertionError and enters the <error message expression>.
Code
Public class AssertDemo {public static void main (String args []) {// true, continue to execute assert true; System. out. println ("continue"); System. out. println ("-------- I am a gorgeous split line ------"); // false, the program terminates assert false: "EXCEPTION"; System. out. println ("continue ");}}
I am the dividing line of tiantiao
- The assert keyword must be explicitly enabled at run time to take effect. Otherwise, your assert has no meaning.
- Assert cannot replace if. The assert judgment is similar to the if statement, but there is an essential difference between the two: the assert keyword is intended for testing and debugging programs, however, if you accidentally use assert to control the Business Process of the program, removing the assert keyword after testing and debugging means modifying the normal logic of the program.
- Assert failure will cause the program to exit.