Understand and correctly use the assertion in Java (Assert)

xiaoxiao2021-03-06  88

Understand and correctly use the assertion in Java, grammar form: Java2 added a keyword in 1.4: assert. Use it to create an assertion during program development, its grammar forms have two forms as shown below: 1. Assert Condition; Here Condition is an expression that must be true (TRUE). If the result of the expression is true, the assertion is true, and no action If the expression is false, the assertion fails, then an AssertionError object will throw an assertionError. This assertionError inherits in an Error object, and Error inherits on throwable, error is a wrong object in parallel with Exception, usually used to express system-level running errors. 2, askER CONDITION: EXPR; here is the same as above, this colon follows an expression, usually used to assert the prompt information after the failure, is white, it is a value that is transmitted to the assertionerror constructor, if asserted Failure, this value is converted to its corresponding string and is displayed. Second, use example: The following is an example of using Assert:

Public class teststert {public static void main (string name = "abner chai"; // string name = null; assert (name! = null): "Variable Name is empty"; system.out. Println (name);}}

In the above program, when the variable Name is null, an assertionerror will be thrown and an error message is output. To make the assertions in the program in the program valid and correctly compiled, you must use the -source 1.4 option when compiling the program. Such as: Javac-Source 1.4 Testassert.java In the Eclipse (3.0m9) development environment, you must select "Java-> Compiler" on Window-> Preferences, select "Compiler Compliance" on the "CompLiance and ClassFiles" page on the right. "Select to 1.4; at the same time," Use Default Compiler Settings "goes out. And select the following "generated .class file compatibility" and "Source Compatibility" to 1.4 to compile correctly. At the same time, if you want to assert the start-up, that is, let the assertion statement really check at runtime, when running the program with Assert, you must specify the -ea option, to run the above program, we perform the following code: Java -ea Testassert In the Eclipse (3.0m9) development environment, Testassert is running, we must configure the runtime option "run", and fill in the-EA option in "VM Arguments" in the Arguments page. Let the assert that the assert is active at runtime. Third, notes: Understand the most important point of comprehension is that they must do not rely on their actual behavior required to complete any program. The reason is that the normal published code is invalid, that is, the normal release code is not executed (or does not work), if you are not careful, we can use assertions in error, such as: Public Class TestPerson {private String name = null; public TestPerson (String name) {this.name = name;} public void setName (String nameStr) {this.name = nameStr;} public String getName () {return this.name;} public static void main (String [] args) {TestPerson personObj = new TestPerson ( "Abner Chai"); String personName = null;! assert (personName = personObj.getName ()) = null; System.out.println (personName.length ( );

In this program, the assignment of Personname is transferred to the assert6 statement, although it can run well when it is asserted (ie, if you run with -ea runtime) but if the assertion is invalid, it will run the empty pointer error . Because when the assertion is invalid, Personname = personobj.getName () will never be executed! As a result, it is a good condition for Java because they enable the error type in the development process to flow, for example, the above program is not allowed to confirm that Personname is not empty, then: if (personname! = Null ) {System.out.println (Personname.length ());}. With ASSERT, use Assert, just one line of code, and do not have to remove the Assert statement from the published code. So, the program above, after the correction, we can use Assert, as follows: public class testperson {private string name = null; public testperson (String name) {this.name = name;} public void setname (String nameStr) {this.name = nameStr;} public String getName () {return this.name;} public static void main (String [] args) {TestPerson personObj = new TestPerson ( "Abner Chai"); String personName = NULL; personname = personobj.getname (); assert personname! = null; system.out.println (Personname.Length ());}}

Fourth, other options: When executing the code, use the -ea option to make the assertion valid, or use the -DA option to make the assertion is invalid (default is invalid), or by specifying the package name after -EA or -DA. The assertion of the package is effective or invalid. For example, to make the assertion in a com.test package is invalid, you can use: -da: com.test To make assertions in all subcles in a package can be effective or invalid, add three points after the package name. For example: -ea: com.test ... You can make the com.teest package and the assertion in the sub-package. Copyright by Abnerchai, 2004. You Can CAN Contact Me At: abnerrtsai@sina.com

转载请注明原文地址:https://www.9cbs.com/read-122581.html

New Post(0)