Java assert keyword is used to create assertions in Java, which enables us to test the assumptions about our program. For example, an assertion may be to make sure that an employee’s age is positive number.
Each assertion contains a boolean expression that you believe will be true
when the assertion executes. If it is not true, the system will throw an error.
1. Java assert syntax
The assertion statement will have either of below given two forms:
assert expression1; //or assert expression1 : expression2 ;
Here –
- expression1 is a boolean expression.
- expression2 is an expression that has a value and this value will be compared with expression1.
The second form of the assertion statement should be used in preference to the first only when the program has some additional information that might help diagnose the failure.
Like all uncaught exceptions, assertion failures are generally labeled in the stack trace with the file and line number from which they were thrown.
2. Java assert example
assert
can help support in design-by-contract style of programming. It can be used to validate the pre-conditions, post-conditions and other general assertions. e.g
import com.howtodoinjava.Employee; public class Main { public static void main(String[] args) { Employee e = new Employee(1, "lokesh", "gupta"); //Passes successfully assert e.getFirstName().length() < 20 : e.getFirstName().length(); //Failed assert e.getLastName().length() < 5 : e.getLastName().length(); } }
Program output.
Exception in thread "main" java.lang.AssertionError: 5 at com.howtodoinjava.Main.main(Main.java:15)
In above example, look at the assertion error message. It prints ‘5’, which tells that which parameter value was tested and failed. It helps in debugging.
3. Enable and disable assertions
Sometimes assertions may increase the execution overhead of the program. To ensure that assertions are not a performance liability in deployed applications, assertions can be enabled or disabled when the program is started, and are disabled by default. Once disabled, they are essentially equivalent to empty statements in semantics and performance.
- To enable assertions at various granularities, use the
-enableassertions
, or-ea
, switch. - To disable assertions at various granularities, use the
-disableassertions
, or-da
, switch.
The granularities can be defined like below:
-
no arguments
Enables or disables assertions in all classes except system classes.
java -ea Main
Above command will run
Main.class
with assertions enable on all the classes. -
packageName…
Enables or disables assertions in the named package and any subpackages.
java -ea:com.howtodoinjava.dao... Main
Above command will run the
Main.class
, with assertions only enabled in packagecom.howtodoinjava.dao
. -
…
Enables or disables assertions in the unnamed package in the current working directory.
java -da:... Main
Above command will run
Main.class
with assertions disables on all the classes present in current working directory whereMain.class
is present. -
className
Enables or disables assertions in the named class only.
java -ea:com.howtodoinjava.dao... -da:com.howtodoinjava.dao.RolesDao Main
Above command will run the
Main.class
, with assertions enabled in packagecom.howtodoinjava.dao
; BUT not in classcom.howtodoinjava.dao.RolesDao
.
Happy Learning !!