Java Assertion

0
111

This tutorial is all about Java Assertion or 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:
[java]

class A {

public static void main (String args[]) {

System.out.println (“Begin”);

assert false ; // very simple assert statement. It will not execute as this //

// is disabled by default.

System.out.println (“End” );

}

}
[/java]

 

Facebook Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.