Assertion keyword in Java
- 17th Aug 2017
- 0
- 4215
- assert keyword in java assertion in java assertion keyword with example how to enable assertion in java how to use assert keyword in java keyword assert with an example when to use assert when to use assertion why do we need assert keyword in java why do we need assertion in java
An assert
is a keyword in Java which enables us to test our assumptions about our program.
Assert will throw exception, If its expression evaluates to false and do nothing if it’s true.
Syntax :
assert Expression1 ;
Where,
Expression1 is a boolean expression.
When the system runs the assertion, it evaluates Expression1 and if it is false then throws an AssertionError with no detail message.
The second form of the assertion statement is:
assert Expression1 : Expression2 ;
Where,
Expression1 is a boolean expression.
Expression2 is an expression that has a value. (It cannot be an invocation of a method that is declared void.)
Expression2 is used as a detailed error message if it exists.
Consider the below example
If we are calculating the total salary of all employees in a company and we need to assure that total salary of all employees will not exceed 10 lacs.
Then we can use assert as below
assert totalSalary <= 1000000 : “Total salary should not be greater than 10 lacs”;
Each assertion contains a boolean expression that we assume will be true when the assertion executes. If it is not true, the system will throw an error.
By verifying that the boolean expression is indeed true, the assertion confirms our assumptions about the behavior of our program, increasing our confidence that the program is free of errors.
How to enable the assertion?
- public class Employee {
- public static void main(String[] args) {
- // Assume below method call gets total salary
- float totalSalary = getTotalSalary();
- assert totalSalary <= 1000000:”Total Salary should not be more than 10 lacs”;
- // stops if totalSalary is greater than 10 lacs
- System.out.println("Success");
- }
- }
public class Employee { public static void main(String[] args) { // Assume below method call gets total salary float totalSalary = getTotalSalary(); assert totalSalary <= 1000000:”Total Salary should not be more than 10 lacs”; // stops if totalSalary is greater than 10 lacs System.out.println("Success"); } }
Assertion in java is disabled by default and the same can be enabled as below
java –ea class name
java –ea Employee
where,
–ea is enableassertion
Employee is the class name.
Use case 1 :
Java –ea Employee
totalSalary is 7 lacs then if we run the above program by enabling assertion
This will print the output as “success”.
Use case 2:
Java –ea Employee
totalSalary is 11 lacs then if we run the above program by enabling assertion
This will throw an assertion error as below
Exception in thread "main" java.lang.AssertionError : Total Salary should not be more than 10 lacs
When to use assertion?
Assertions should be used to check something that should never happen in our application and we can use it whenever we want to verify our assumptions with the pre-conditions in our code.