In this post, I am going to explain about the assert statement in Java with the help of examples.
Assertions were added in Java1.4, that enables you to test the assumptions about your application. Even though assert keyword was added in Java1.4, it is still a little-known feature of the language.
Where can I use assertions?
For example, you write a method to calculate the sum of two numbers, you might assert that the calculated sum is not infinity.
Assertions are used to
a.
Find
the bugs at the earliest during development
b. Document the inner workings of your program.
Assert statement has two forms.
Form 1
assert Expression1 ;
If the expression1 is not evaluated to true, system will throw an error.
App.java
package com.sample.app;
public class App {
public static boolean isEqual(Object obj1, Object obj2) {
assert obj1 != null;
return obj1.equals(obj2);
}
public static void main(String[] args) {
isEqual(null, "Hello World");
}
}
Run above application, you will end up in NullPointerException.
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.equals(Object)" because "obj1" is null at com.sample.app.App.isEqual(App.java:7) at com.sample.app.App.main(App.java:11)
Wait…what happened to my assertion, which is supposed to check obj1 != null, before calling equals method. It is because, assertions are not enabled by default in Java.
How to enable assertions in Java?
Use the option -ea or -enableassertions to enable assertions in your application.java –ea App (or) java –enableassertions App
Run the application by enabling the assertion, you will get assertion error.
Exception in thread "main" java.lang.AssertionError
at com.sample.app.App.isEqual(App.java:6)
at com.sample.app.App.main(App.java:11)
Form 2
assert Expression1 : Expression2 ;
In this form,
a. Expression1 is a Boolean expression which is evaluated to either true or false.
b. Expression2 is an expression that has a value. (It cannot be an invocation of a method that is declared void.)
Form2 syntax is used to provide an additional message about the error.
Example
assert obj1 != null : "obj1 must not be null";
App.java
package com.sample.app;
public class App {
public static boolean isEqual(Object obj1, Object obj2) {
assert obj1 != null : "obj1 must not be null";
return obj1.equals(obj2);
}
public static void main(String[] args) {
isEqual(null, "Hello World");
}
}
Output
Exception in thread "main" java.lang.AssertionError: obj1 must not be null at com.sample.app.App.isEqual(App.java:6) at com.sample.app.App.main(App.java:11)
Enable and disable assertions at various granularities
a. Use the option -enableassertions, or -ea to enable assertions at various granularities.
b. Use the option -disableassertions, or -da to disable assertions at various granularities.
Following table summarizes the various granularities supported by Java.
Granularity |
Description |
no arguments |
Enables or disables assertions in all classes except system classes. |
packageName... |
Enables or disables assertions in the named package and any subpackages. |
... |
Enables or disables assertions in the unnamed package in the current working directory. |
className |
Enables or disables assertions in the named class |
Example 1: Following command runs HelloWorld program, with assertions enabled in only package com.sample.app.services and its subpackages:
java -ea:com.sample.app.services... HelloWorld
Example 2: Following command runs the HelloWorld program with assertions enabled in package com.sample.service but disabled in the package com.sample.service.arithmetic.
java -ea:com.sample.service... -da:com.sample.service.arithmetic HelloWorld
Example 3: Following command runs the HelloWorld program with assertions enabled in the clas com.sample.app.MyClass.
java -ea com.sample.app.MyClass HelloWorld
I would suggest you to read this post formore details on when to use assertions and when not to use assertions.
You may like
How to check whether a class is loaded or not in Java?
How to get a random element from Set?
Extract the elements of specific type in a collection
No comments:
Post a Comment