Use of Assertions in Java

Introduction

 
Assertions were added in Java 1.4 to create reliable programs that are correct and robust.  Assertions are Boolean expressions that are used to test/validate the code.  They are basically used during the testing and development phases.  Assertions are used by programmers to be doubly sure about a particular condition, which they feel to be true.
 

Declaration of Assertions

 
assert Expression1
 
In Java, we declare Assertions with the help of the assert keyword.  In this syntax, Expression1 is a Boolean expression; when the program is executed, the Expression1 in the assert is checked.  If Expression1 returns true, then the assertion set is true and the program runs without interruption.  In the case when Expression1 returns false, then an AssertionError is thrown and the Assertion makes the program fail.

assert
Expression1: Expression2
 
Note
In this syntax, Expression1 has the same meaning as we explained above.  The second Expression2 defines a string type statement that you want to show as an error message.  This string type message is passed to the constructor of the AssertionError Object.  If the Expression1 returns false, then the Expression2 value returns an error message.

public AssertionError()

This is the hierarchy of the AssertionError class in Java:
 
java.lang.Object
java.lang.Throwable
java.lang.Errorjava.lang.AssertionError
 
Note
Assertions must be enabled explicitly; they are disabled by default.  Using the -ea and -da option of Java, we can enable or disable assertions.
 
Example
  1. class MyAssertion    
  2.   {    
  3.         
  4.   static void ErrorCheck(int i)    
  5.      {    
  6.         
  7.     assert i>0:"value must be Enter a possitive nuber";    
  8.     System.out.println("you Enter a valid number ="+i);    
  9.      }    
  10.   public static void main(String arg[])    
  11.    {    
  12.    ErrorCheck(Integer.parseInt(arg[0]));    
  13.    }    
  14.  }      
OUTPUT
 
Step 1: When -ea is used and enter the value throw command line is -1.
 
Myessertionwith -1.gif
 
Step 2: When -ea is used and enter the value throw command line is 5, the output will be:
 
myessertioncmd with 5.gif
 
Step 3: Without using -ea the output will be the same for both numbers -1 and 5. 
 
withoutuse -ea keyword.gif
 
Example
 
This example is made by using the assert keyword, but using a single argument form like assert Expression1:
  1. class MyAssertion1 {    
  2.     
  3.     static int maxmarks = 100;    
  4.     
  5.     static int changes(int mark) {    
  6.         maxmarks = maxmarks - mark;    
  7.         System.out.println("maxmark:= " + maxmarks);    
  8.         return maxmarks;    
  9.     }    
  10.     
  11.     public static void main(String args[]) {    
  12.         int g;    
  13.         for (int i = 0; i < 5; i++) {    
  14.             g = changes(15);    
  15.             assert maxmarks >= 70.00;    
  16.         }    
  17.     }    
  18. }      
OUTPUT
 
Without using -ea, the output will be the same for both numbers -1 and 5. 
 
myessertion1cmd.gif
 
Resources


Similar Articles