The assert statement
Once in a while, a programmer needs to know whether a particular condition has happened in the code, even after the application has already been deployed to production. At the same time, there is no need to run this check all the time. That is where the assert
branching statement comes in handy. Here is an example:
public someMethod(String s){
//any code goes here
assert(assertSomething(x, y, z));
//any code goes here
}
boolean assertSomething(int x, String y, double z){
//do something and return boolean
}
In the preceding code, the assert()
method takes input from the assertSomething()
method. If the assertSomething()
method returns false
, the program stops executing.
The assert()
method is executed only when the JVM is run using the -ea
option. The -ea
flag should not be used in production, except maybe temporarily for testing purposes...