Assertion in Java – Java Assert Keyword
Here, in this Java Assert tutorial, we are going to learn about what is an assertion in Java. Moreover, we will study how to enable and disable assertion in Java, Why to use Java assertions and the difference between Java assertion vs. normal exception handling.
At last, we will discuss where to utilize assertion in Java and assertion examples.
What is Assertion in Java?
Java assertion allows us to test or check the correctness of any assumptions that have been made in the program. We can achieve it using the assert statement in Java. While executing an assertion, we assume it to be true.
If the assertion fails, then JVM throws AssertionError error. Consequently, we use it for testing purposes during development. We can use an assert statement with a Boolean expression and can write in two different ways which are as follows:
- Assert expression;
- Assert expression1: expression2;
History of Java Assert
Java introduced the Java assert keyword in Java 1.4. However, this keyword remains a little-known keyword that can drastically reduce boilerplate and can provide more readability to our code.
For example, we often need to verify certain conditions in our code that might prevent our application from working properly. Typically we would write something like this:
Connection c = getConnection(); if(c == null) { throw new RuntimeException("Connection is null"); }
Using assertions, we can remove the ‘if’ statement and throw statement with a single assert statement.
Example of Java Assert
import java.util.Scanner; class Test { public static void main( String args[] ) { int value = 15; assert value >= 20 : " Underweight"; System.out.println("value is "+value); } }
Enabling and Disabling Assertions in Java
We can enable Java Assert by default. The syntax for the same is:
java –ea Test
Or
Java –enableassertions Test
The syntax for disabling Java Assert-
java –da Test
Or
java –disableassertions Test
Why use Java Assertion
Wherever a software engineer needs to check whether his/her suspicions are right or not,
- To ensure that an inaccessible code is really inaccessible.
- Ensure that presumptions written in the comments are correct.
- To ensure that the default switch case is not reached.
- Check the state of the object.
- At the start of the method.
- After the invocation of the method.
Java Assertion vs Normal Exception Handling
We mainly use Assertions to check logically impossible situations. For example, we can use them to check the state a code expects before it starts running or state after it finishes running.
The difference between assertions and normal exception handling is that the assertions are generally disabled at run-time.
Where to Utilize Assertion in Java
We can utilize Java Assert in the following ways:
- Firstly, we can use Java Assertions in arguments for private methods. The developer’s code only provides the private arguments and the developer may want to check his/her assumptions about arguments.
- Secondly, assertions can also be utilized in Conditional cases.
- Lastly, we can use them in the Conditions at the beginning of any method.
Where Not to Utilize Assertion in Java
There are many situations where we should not attempt to use Java Assertions:
- To replace the error messages.
- For checking arguments in the public methods as they may be provided by the user.
- To handle errors provided by the user, for this we must use error handling
- lastly on command line arguments.
Handling an AssertionError
The class AssertionError extends Error class and the Error class itself extends Throwable. This means that the AssertionError is an unchecked exception.
Therefore we do not need to declare the methods that use assertions and also further calling code needs not try and catch them. We use AssertionErrors to indicate unrecoverable conditions in an application, so we should never try to handle them or attempt recovery.
Conclusion
Hence, in this Java tutorial, we studied what is an assertion in Java, enabling and disabling it, why use Java assertion. We also studied the difference between Java assertion and normal exception handling and how to utilize java assert.
We also discussed some Java Assert examples. This was all about Java Assert Tutorial. I hope you like our explanation of Assertion in Java.