X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Assertions :

A very common way to debugging a program is by using System.out.print statements. But the problem with it is that after fixing the problem compulsory we should delete these statements otherwise these statements will be executed at runtime and effects performance and disturbs logging. To resolve such problem, Assertions concept is introduced in Java 1.4 version. Assertion allows the programmer to find out if the program behaves as expected. Hence, the main objective of assertion is to perform debugging.

The main advantage of assertion over System.out.print statement is after fixing the problem it is not required to delete assert statement because assertion will be disable automatically at runtime. Based on our requirement we can enable and disable assert statements (i.e. user has the option to turn assertion on or off when running the application) and by default assertion is disable.


'assert' as a keyword & identifier :

'assert' keyword introduced in Java 1.4 version. Hence from 1.4 version onwards we can't use assert as identifier. But before 1.4 version we can use assert as identifier.

Example :

class AssertExample { public static void main(String[ ] args) { int assert = 10; System.out.println(assert); } }

Output :

Compile time Error : as of release 1.4, 'assert' is a keyword, and may not be used as an identifier (use -source 1.3 or lower to use 'assert' as an identifier)

Types of assert statements :


  • Simple version




  • Augmented version




Simple version :-

Syntax : assert (condition);


Condition should be of boolean type.

If the condition is true, then our assertion satisfied and rest of the program will be executed normally.

If condition is false, then our assertion fails and the program will be terminated by raising runtime exception saying AssertionError. So that we can be able to fix the problem.

Example 1 :

class AssertExample { public static void main(String[ ] args) { int x = 10; assert(x == 10); System.out.println(x); } } //Compile as : javac AssertExample.java //Run as : java -ea AssertExample

Output :

10 Note :

During runtime we used '-ea' to enable assertion.

Example 2 :

class AssertExample { public static void main(String[ ] args) { int x = 10; assert(x > 10); System.out.println(x); } } //Compile as : javac AssertExample.java //Run as : java -ea AssertExample

Output :

Runtime Error : java.lang.AssertionError Note :

During runtime we used '-ea' to enable assertion.


Augmented version :-

Syntax : assert (condition) : Description;


We can augment some description by using augmented version to the AssertionError.

Description can be of any type but recommended to use String type.

Example :

class AssertExample { public static void main(String[ ] args) { int x = 10; assert(x > 10) : "Here 'x' value should be greater than 10 but it is not"; System.out.println(x); } } //Compile as : javac AssertExample.java //Run as : java -ea AssertExample

Output :

Runtime Error : java.lang.AssertionError: Here 'x' value should be greater than 10 but it is not Note :

During runtime we used '-ea' to enable assertion.


Description will be evaluated if and only if condition is false i.e. if condition is true then description won't be evaluated.

Example 1 :

class AssertExample { public static void main(String[ ] args) { int x = 10; assert(x == 10) : ++x; System.out.println(x); } } //Compile as : javac AssertExample.java //Run as : java -ea AssertExample

Output :

10 Note :

During runtime we used '-ea' to enable assertion.

Example 2 :

class AssertExample { public static void main(String[ ] args) { int x = 10; assert(x > 10) : ++x; System.out.println(x); } } //Compile as : javac AssertExample.java //Run as : java -ea AssertExample

Output :

Runtime Error : java.lang.AssertionError: 11 Note :

During runtime we used '-ea' to enable assertion.


We can take a method call as description but void type method calls are not allowed.

Example :

class AssertExample { public static void main(String[ ] args) { int x = 10; assert(x > 10) : algo(); System.out.println(x); } public static String algo() { return "Algo"; } } //Compile as : javac AssertExample.java //Run as : java -ea AssertExample

Output :

Runtime Error : java.lang.AssertionError: Algo Note :

If algo() return type is void then we will get compile time error saying : 'void' type not allowed here.

During runtime we used '-ea' to enable assertion.


Various runtime flags :


-ea :

This is use to enable assertion in every non-System class.


-enableassertions :

This is also use to enable assertion in every non-System class.


-da :

This is use to disable assertion in every non-System class.


-disableassertions :

This is also use to disable assertion in every non-System class.


-esa :

This is use to enable assertion in every System class.


-enablesystemassertions :

This is also use to enable assertion in every System class.


-dsa :

This is use to disable assertion in every System class.


-disablesystemassertions :

This is also use to disable assertion in every System class.


Appropriate and inappropriate use of assertions :


  • It is always inappropriate to mix programming logic with assert statement because there is no guarantee of execution of assert statement at runtime.

  • In our program if there is any place where the control not allowed to reach then it is best place to use assert statement.

  • It is always inappropriate to use assertions for validating public method arguments.

  • It is always appropriate to use assertions for validating private method arguments.

  • It is always inappropriate to use assertions for validating command line arguments because these are arguments to public main().

AssertionError :

It is the child class of Error and hence it is unchecked.

It is legal to catch AssertionError by using try-catch but it is not recommended.

Example :

class AssertExample { public static void main(String[ ] args) { int x = 10; try { assert(x > 10); } catch(AssertionError ae) { System.out.println("AssertionError caught"); } } } //Compile as : javac AssertExample.java //Run as : java -ea AssertExample

Output :

AssertionError caught Note :

During runtime we used '-ea' to enable assertion.



X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.