Assertions in Java

This article will take you deep into the concepts of assertions in java, their uses and various sample programs. Let’s start!!!

Assertions in java:

The presence of assertions in Java plays a vital role in testing the programmer’s assumptions regarding the program. During the execution of assertion, it is considered to be true. In case it fails, the JVM throws an error names AssertionError. The assertion is mostly used for the purpose of testing during development.

The assert statement in Java lets us achieve this stage.

There are two ways to use an assert statement:

1. assert expression;

2. assert expression1: expression2

Enabling assertions in Java:

By default, assertions are disabled in Java. To enable them, we must use the commands given below:

  • java -ea Example

(or)

  • java -enableassertions Example

Here, Example is the name of the Java file.

Sample program to implement assertion in Java:

public class FirstCode{
   public static void main(String[] args) {
  	int percentage = 14;
  	assert percentage <= 40 : "Fail";
  	System.out.println("The student's result is: " + percentage);
   }
} 

Output:

Fail

Why should we use java assertions?

When a programmer is perplexed to know if his/her assumptions are right or wrong, they can use assertions.

  • It is also used to ensure that a code that seems to be unreachable is indeed unreachable.
  • It is also used to ensure that the assumptions given in the comments are correct.
if ((x & 1) == 1) {  
}  
else // x must be even  
{  
assert (x % 2 == 0);  
}  
  • To ensure that the default switch case is not in use.
  • To check the state of the object.
  • It is present at the beginning of a method.
  • It is also used during the method of invocation.

Assertion vs Normal exception handling in Java:

Normally, assertions verify the situations that the logically possible. For example, it checks the state that a code expects before it begins to run. In the same way, it also checks the state of the code after it finishes running too.

Unlike normal exception or error handling, assertions are usually disabled during run time.

When to use java assertions:

  • Arguments to private methods. Only the developer would want to check the assumptions about the arguments given in the code.
  • Conditional cases.
  • Conditions at the beginning of a method.

Where not to use java assertions:

  • We should not use assertions to replace error messages.
  • It should not check arguments in the public methods as they are given by the user. Including Error handling methods is vital as it handles the errors that the users give.
  • Assertions should not be used on command line arguments.

Advantage of java assertion:

The main advantage of using assertions is that it lends an effective way to identify and correct errors in the program.

Conclusion

I hope this article made you clear about assertions in Java. You can try using this concept in your program for better quality code.

Leave a Reply

Your email address will not be published. Required fields are marked *