The Assert keyword for Java traps

Source: Internet
Author: User
Tags assert terminates java web java se

the Assert keyword for Java traps I. OverviewIn the C and C + + languages, there are assert keys, which represent assertions. In Java, there is also an ASSERT keyword, which means that assertions are similar in usage and meaning. second, the grammarIn Java, the Assert keyword was introduced from Java SE 1.4, in order to avoid using the Assert keyword in the old version of Java code to cause an error, Java does not start the assertion check by default (at this point, all assertion statements will be ignored!). ), if you want to turn on assertion checking, you need to turn on the switch-enableassertions or-ea. The Assert keyword syntax is simple and there are two ways to use it:1. Assert <boolean expression >If the <boolean expression > is True, the program continues execution. If False, the program throws a assertionerror and terminates execution.2. Assert <boolean expression >: < error message expression >If the <boolean expression > is True, the program continues execution. If False, the program throws a Java.lang.AssertionError and input < error message expression >. third, application examplesHere is an example of how to use the example: Public classAssertfoo { Public Static voidMain (String args[]) {///assertion 1 result is true, continue execution down assert true; System. out. println ("Assertion 1 no problem, go!         "); System. out. println ("\ n-----------------\ n"); Assertion 2 result is false, program terminates assert false: "The assertion failed, and the information for this expression will be output when the exception is thrown!"        "; System. out. println ("Assertion 2 no problem, go!    "); Save the code to C:\AssertFoo.java, and then follow the instructions below to see the console output: 1. Compile the program:C:\>javac Assertfoo.java 2, the default execution program, does not open-ea switch:C:\>java Assertfoo Assertion 1 No problem, go! -----------------Assertion 2 No problem, go! 3. Turn on the-EA switch and execute the program:C:\>java-ea Assertfoo Assertion 1 No problem, go! -----------------Exception in thread "main" Java.lang.AssertionError:The assertion fails, and the information for this expression iswill output when an exception is thrown! At Assertfoo.main (assertfoo.java:10) Iv. PitfallsThe Assert keyword is simple to use, but using assert often causes you to fall into a deeper trap. Use should be avoided. The author after the study, summed up the following reasons: 1, the Assert keyword needs to be explicitly opened at runtime to take effect, otherwise your assertion will have no meaning. Now the mainstream Java IDE tool does not have the-ea assertion check feature turned on by default. This means that if you use the IDE tool to encode, debugging run time will be some trouble. And, for Java Web applications, program code is deployed in the container, you can not directly go to the control program, if you must turn on the-EA switch, you need to change the Web container run configuration parameters. This has caused great inconvenience to the transplant and deployment of the program. 2, the use of assert instead of if is the second trap. The Assert's judgment is similar to the IF statement, but the role of the two is essentially different: The Assert keyword is intended to be used for testing the debugger, but if you accidentally control the program's business process with assert, The removal of the Assert keyword after test debugging means that the normal logic of the program is modified. 3, Assert assertion failure will face the program exit. This application in a production environment can never be tolerated. It is common to use exception handling to resolve potential errors in the program. But using assertions is dangerous, and once the system fails, it hangs. Five, the thinking of the AssertSince the assert is intended to debug a test program and is not used in a formal production environment, it should be considered better to test juint instead of using it, and the juint relative to the Assert key provides more functionality. Of course, debug testing can be done entirely through IDE Debug. In this view, the future of the assert is dim. Therefore, you should avoid using the Assert keyword in Java, unless the Java default supports the switch to turn on-ea, which can be considered. In contrast, how much of an assert can bring you, how much trouble, is the principle we choose to use.above only represents my personal views, welcome to leave a message to discuss.

The Assert keyword for Java traps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.