I'd like to know how Java would handle the following scenario:
Suppose I have a class called Debug, which looks like this:
public class Debug
{
private static final boolean isAssertEnabled = true;
public static void assertTrue(boolean b, String errorMessage) {
if (isAssertEnabled) {
if (!b) {
throw new RuntimeException(errorMessage);
}
}
}
}
and suppose my code has a call that looks something like this:
...
Debug.assertTrue((x + y != z) && (v - u > w), "Some error message");
....
I have a few questions:
- If the isAssertEnabled flag is set to false, will the whole call to Debug.assertTrue be compiled out? Please note that the check if isAssertEnabled == true is only made inside the function after it was called.
- If the whole call does get compiled out, does that also mean that the evaluation of the boolean expression is compiled out? Would be a waste to evaluate that expression for nothing.
Thanks for you help!