ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Java Assertion

Assertion in Java was introduced in JDK 1.4. Assert is a keyword.

There are two types of assert statements :

  1. very simple assert statement.
    assert <boolean_result>; (After assert there should be a space )
  2. simple assert statement.
    assert< boolean_result> :some literal

By default assert statements will not execute because they are disabled. In order to execute assert statements, we need to supply one of the VM argument:

ea or enable assertions

In order to disable assertion statement explicitly we need to supply another VM argument :

da or disable assertions

 

By default , every assert statement got disable.

 

REQUIREMENT OF ASSERTION IN JAVA:

  • assertions are used to fix business rules.If at all assert statement is receiving boolean result as “false ” , we will get assertion error. Assertion error is a type of error of a type of Error class.

Example:

  1. class A {
  2.  
  3. public static void main (String args[]) {
  4.  
  5. System.out.println ("Begin");
  6.  
  7. assert false ; // very simple assert statement. It will not execute as this //
  8.  
  9. // is disabled by default.
  10.  
  11. System.out.println ("End" );
  12.  
  13. }
  14.  
  15. }

 

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!