(i) First clear:
The Java assertion assert is introduced by jdk1.4.
The JVM assertion is closed by default.
Assertions can be locally opened, such as when the parent class prohibits assertions, and subclasses open assertions, so it is generally said that "assertions are not inheritable."
Assertions apply only to complex debugging processes.
Assertions are generally used to determine the results of program execution, and do not let assertions handle business processes.
( B) to determine whether Eclipse has opened the assertion, the code is as follows:
public class Asserttest {
public static void Main (string[] args) {
Boolean isOpen = false;
Assert isopen=true; If the assertion is turned on, the IsOpen value is changed to True
SYSTEM.OUT.PRINTLN (IsOpen)//printing whether the assertion is turned on
}
}
Executes the above code, if the assertion is enabled if the print is true, and if false, the assertion is not enabled.
If the assertion is not enabled, then open the assertion as described below.
(iii) Open assertions in Eclipse
Select menu:run ---> run ... ---> select Arguments tab
In the VM arguments text box, enter: -ea Note that there are no spaces in the middle, if the input -da indicates that the assertion is prohibited.
Then close the window, prompt for saving, and then save to open the assertion.
Perform the second step again, and if the print is true, the assertion is turned on successfully.
If the assertion is prohibited, in the second step, delete the-ea or change the-ea to-da.
(iv) asserts the use
The 1th method of use:
Assert <boolean expression >
If the <boolean expression > is True, the program continues to execute.
If False, the program throws a assertionerror and terminates execution.
public static void Main (string[] args) {
Boolean isOk = 1>2;
Assert isOk;
SYSTEM.OUT.PRINTLN ("normal procedure");
}
Because 1>2 is clearly wrong, the execution result throws an exception: Exception in thread "main" Java.lang.AssertionError
If you change the 1>2 to 1<2 then the program will execute smoothly, print "normal program"
The 2nd method of use:
Assert <boolean expression >: < error message expression >
If the <boolean expression > is True, the program continues to execute.
If False, the program throws a java.lang.assertionerror,< error message expression > becomes a assertionerror.
public static void Main (string[] args) {
Boolean isOk = 1>2;
try{
Assert isOk: "Program error";
SYSTEM.OUT.PRINTLN ("normal procedure");
}catch (Assertionerror err) {
System.out.println (Err.getmessage ());
}
}
The assert is followed by a colon expression. If true before the colon, it is ignored after the colon.
If the colon is false, Assertionerror is thrown, the error message content is after the colon, and the result of the above program is print: "Program error"
(v) Additional parameters relating to assertions (reprint):
-ea Java-ea Open Assertion for all user classes
-da Java-da Closes all user classes assertion
-ea:<classname> Java-ea:myclass1 Open the MyClass1 assertion
-da:<classname> Java-da:myclass1 closes the MyClass1 assertion
-ea:<packagename> JAVA-EA:PKG1 Open the PKG1 package assertion
-da:<packagename> Java-da:pkg1 Closes the PKG1 pack assertion
-ea: ... Java-ea: ... Open the default package (Nameless package) assertion
-da: ... java-da: ... Turn off the default package (Nameless package) assertion
-ea:<packagename> ... java-ea:pkg1 ... Open the assertion of the PKG1 package and its child packages
-da:<packagename> ... java-da:pkg1 ... Turn off the assertion of the PKG1 package and its child packages
-esa Java-esa Open the assertion of the system class
-DSA JAVA-DSA shuts down the system class assertion
Comprehensive use of JAVA-DSA:MYCLASS1:PKG1 to close MyClass1 and PKG1 packs assertion
Refer:http://blog.sina.com.cn/s/blog_59c9412d0100fd55.html