Assertion in Java

Source: Internet
Author: User
An
Assertion is a statement in the Java language that enables us to test our assumptions about our program. for example, if we write a method to calculate the money in our pocket, then we might assert that the total value is more than 0.
1. Assert forms. 1) assert Expression1; Where Expression1Is a Boolean expression. When the system run the assert, it evaluate expression, and if it is false, it wocould throw assertionerror with no detail message.
Pls noted that assertionerror inherit from error 2) assert Expression1: Expression2; Where:
  •  Expression1Is
    A boolean expression.
  • Expression2Is an expression that has a value. (It cannot be an invocation of a method that is declared void .)
Pls noted that assertionerror inherit from error, so we may HV no need to handle, also may catch it as normal error. 2. Putting assert into code.1) Internal invariants you shocould now Use an assertion whenever you wowould have written a comment that asserts an invariant. If (I % 3 = 0) {...} else if (I % 3 = 1) {...} else { Assert I % 3 = 2: I;...} 2) control-flow invariants it points out another general area where you should use assertions: Place an assertion at any location you assume will not be reached. Void Foo () {for (...) {If (...) return ;} Assert false; // execution shold never reach this point!} 3) preconditions. Private  VoidSetdelegationinfointosubmissiondto (...)
{ Assert isf10snapshot. getisfdelegation ()! = NULL;String delegatorcompanyid = isf10snapshot. getisfdelegation (). getdelegatorcompanyid (); submissiondto. setdelegator ( True);... } 3. Enabling/disabling assertions.1) to enable assertions at various granularities, use the-enableassertions, or-ea,
Switch. To disable assertions at various granularities, use the-disableassertions, or-da, switch. you specify the granularity with the arguments that you provide to the switch: · No
Arguments
Enables or disables assertions in all classes except T System Classes .· Packagename...
Enables or disables assertions in the named package and any subpackages .·...
Enables or disables assertions in the unnamed package in the current working directory .· Classname
Enables or disables assertions in the named class2) enable it in productname use-ESA |-enablesystemassertions to enable system assertions; Use-DSA |-disablesystemassertions to disable system assertions 3) enable it in eclipse in run configuration dialog, switch to arguments tab, input-Ea/-da in VM arguments
Filed. 4. performance the assertion mechanic is as following. when the compiler finds an assertion in a class, it adds a generated static final field named $ assertiondiabled to the class. and the assertion itself is compiled
Into a Statement of the form: if ($ assertiondisabled) if (! Boolean_expression) throw new assertionerror (string_expression); so, if assertion overhead, it wocould introduce an performance issue into system, no matter the assertion is enabled or disabled.

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.