Assertion in Java

來源:互聯網
上載者: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 the  Expression1is a Boolean expression. When the system run the assert, it evaluate Expression, and if it is false,  it would throw AssertionError with no detail message.
Pls noted that AssertionError inherit from Error        2) assert  Expression1Expression2;where: 
  •  Expression1 is
    a boolean expression.
  • Expression2 is 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 should now  use an assertion whenever you would 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 should never reach this point!    } 3) Preconditions.  private  void setDelegationInfoIntoSubmissionDto(…)
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 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 oc4j      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 mechanism 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 would introduce an performance issue into system, no matter the assertion is enabled or disabled.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.