Here we have discussed assertion in Java in detail with its types, benefits, syntax and an example that will help beginners to understand its concept and use.Here we have discussed assertion in Java in detail with its types, benefits, syntax and an example that will help beginners to understand its concept and use.
In this section we will discuss about what is assertion, how to use assert keyword and benefits of assertion in Java. We have also provided an example to help beginners understand the concept of assertion in Java. Assertion are assumption or predictions made at the beginning of the program so that to ensure the program is true, otherwise, it will not reach to that point and the program will be terminated and throw an AssertionException error.
Assertion is used to test assumptions in a program and make it valid by catching exceptions and logical errors. It makes make program more readable and user friendly. It validates data at run-time. Assertion contain a Boolean expression which is set to be true in the beginning of the program.
Assertion are of two types:
preconditions- invoked when a method is invoked
postconditions- invoked when a method is finished
Assertion can also be used in control flow, as invariants of a class.
Implementing Assertion in Java
Assertion is carried out in Java by using assert keyword. It can be expressed in two ways:
This statement specifies that if assertion expression becomes false, an AssertionError is thrown and program is terminated
assert expression1 : expression2 :
This statement means that the expression1 is evaluated first and if it is false then an AssertionError error with an additional message from expression2 is thrown.
Benefits of Assertion In Java:
"statement1" is a Boolean expression, which is checked when program is executed. If it returns true, program runs without interruption. If statement1 returns false,, the program throw AssertionError and program gets terminated.
"statement1" is an error message and statement2 is a Boolean expression. statement1 gives an error message if the statement 2 return false, then it is passed to the Assertion Error constructor of the respective error class. The constructor changes the value in statement1 into string format.
Example of Assertion in Java:
Here we have defined a public class 'Mark Assert Demo'. Inside it we have defined the static variable i.e. maximum marks to be 100, changes is another static variable, The main static ( ) method has assumption of maximum marks i.e. 40, if the marks come below to 40, the code will show you java.lang.AssertionError and display the Marks is below than 40 on the command prompt.
public class MarkAssertDemo
static float maximummarks=100;
static float changes(float mark)
System.out.println("The maximummark is:" + maximummarks);
public static void main(String args)
assert maximummarks>=40.00:"marks is below 40.";
C:\saurabh>javac -source 1.4 MarkAssertDemo.java
C:\saurabh>java -ea MarkAssertDemo
The maximummark is:85.0
The maximummark is:70.0
The maximummark is:55.0
The maximummark is:40.0
The maximummark is:25.0
Exception in thread "main" java.lang.AssertionError: marks is below 40.