Assertion is nothing but a condition that should be true while executing the program.
Assertion keyword is mostly used in the development and testing phases. It is used as an alternative debugging mechanism. Prior to the introduction of assert we had to use System.out.println to check whether the code compiles fine without any bug. After fixing the bugs in the program we used to comment the System.out.println lines . Assert help us to reduce this complexity.
Why Assertion ?
Assertion is really helpful for error checking during the development phase,Suppose you want to check for the negative number without using assert we will have to do it in the following way
1 2 3 4 5 |
if(number < 0) { System.out.println("Number is negative"); // need to return some value to indicate the error or throw an exception } |
But if we are using assert
assert number > 0 : “Number is negative”;
Here we can notice that , we need only one line of code and also we don’t have to remove this code before the release as the released code will be running without enabling assertion.
Enabling and disabling assertion for a class
We can enable assertion by prefixing -ea to the class name while executing .
-ea AssertDemo
the same way we can disable assertion as
-da AssertDemo
Enabling and disabling assertion for a package
enabling assert :
-ea : myPackage
Disabling assert :
-da:myPackage
Enabling and disabling assertion for sub packages of a package
-ea:myPackage…
-da:myPackage…
In the above example assertion will be enabled for all the subpackages of myPackage.
How to use Assert ?
There are two ways for creating assert
- assert (condition);
condition should be Boolean type.
If condition is true, there is no bug in the program and executes normally further.
If condition is false, there is a bug and hence the program abnormally terminates with assertion error.
1 2 3 4 5 6 7 8 9 |
public class test { public static void main(String[] args) { boolean x=false; assert(x); { System.out.println("assert is true"); } } } |
javac test.java
java test
Displays output as ‘assert is true’ because assertions are disabled by default
java –ea test
Run time Exception – Assertion error
Here condition is nothing but an expression that should evaluate a Boolean expression. If the result of this condition is true there wont be any action . In case of false result a default AssertionError is thrown.
- assert condition : expr;
In this form we are passing an argument to the AssertionError constructor. If there is any AssertionError happens the expression we are passing will be displaying as part of the AssertionError .
condition should be Boolean type
expr can be any type of description or method calls(except void type)and it is evaluated iff x is false
1 2 3 4 5 6 7 8 9 |
public class test { public static void main(String[] args) { boolean x=false; assert(x): “assert is false”; { System.out.println("assert is true"); } } } |
javac test.java
java test
Displays output as ‘assert is true’ because assertions are disabled by default
java –ea test
Run time Exception – Assertion error:assert is false