Assert is a new feature introduced in J2SE 1.4. Assertion is a Boolean state included in the code. The programmer thinks this status is TRUE. In general, Assert is developed by the inspection program, usually does not use Assert when publishing. Support for Assert keywords and Java.lang.asserterror classes have been added to 1.4.
First of all, we need to speak Assert from an example.
public class AssertTest {public static void main (String [] args) {AssertTest at = new AssertTest (); at.assertMe (true); at.assertMe (false);} private void assertMe (boolean boo) {assert boo true? : false; system.out.println ("True Condition");}} The program contains assert, you have to compile with javac -source 1.4 xxx.java, otherwise the compiler will report an error. If you want the assert to run, use Java-EA XXX to run, otherwise it contains the Assert's destination will be ignored. Here we run javac -source 1.4 AssertTest.java java -ea AssertTest look at the results of the output is: true conditionException in thread "main" java.lang.AssertionError at AssertTest.assertMe (AssertTest.java:13) at AssertTest.main (AssertTest .java: 7) When we run at.assertme (TRUE), due to Assert Boo? true: false is equivalent to Assert True; there is no problem, the program performs printing True Condition, but execute at.assertme (false) The time is equivalent to Assert False. At this time, the interpreter will throw AssertionError, and the program is terminated. Everyone must know that AssertionError is inherited from ERROR, so you can no longer in the program, of course, you can also catch it in the program to continue. For example: public class asserttest {public static void main (string [] args) {assertTest at = new assertTest (); try {at.assertme (true); at.assertme (false);} catch (assertionerror ae) {system. Out.println;} system.out.println ("go on");} private void assertme (boolean boo) {askERT BOO? true: false; system.out.println ("true condition") }}
Assert has another way of expression, it is assert exp1: exp2; where exp1 is a boolean return worth expressing, and EXP2 can be the original data type or object, for example: boolean boo = true; string str = null; assert BOO = false: Str = "error"; we just started to speak assert exp1 form, when EXP1 is FALSE, the AssertionError determines the default constructor to be called, but Assert Exp1: exp2 is in the form of this, when EXP1 is TRUE When it is time, the resulting EXP2 is or slightly, if false, the result of the later expression will be calculated and as an AssertionError parameter. Looking down below: Public class asserttest {public static void main (string [] args) {assertTest at = new assert (); at.assertme (true); at.assertme (false);} private void assertme (Boolean Boo) {String s = null; assert boo? True: false: s = "hello world"; system.out.println ("true condition");}} When running, True ConditionException in Thread "Main" java .lang.assertionerror: hello world at assertt.assertme (asserttest.java:14) at asserttest.main (askTTEST.JAVA: 7) AssertTtertTest.java: 7) ASSERT is best not to abuse, because Assert is not necessarily enabled, the following two situations It is not necessary to check the parameters not to use assert. In the case where the parameters are not for NULL, such as public int GET (String s) {assert s! = Null;} If you need to check, it is best to throw it through if s = null NullPointerException to check that do not use Assert to check the return value of the method to determine the result of the method, such as Assert List.removeall (); this seems to be no problem, but think about if Assert is disable, then he will not be executed. So the removeall () operation is not executed instead of Boolean boo = list.removeal (); assert boo;