The JVM optimizes the if (o! = Null) clause because you have already argued that o will never be null.
Assertion is not enabled by default at runtime and is usually used to verify that this code is executing a contract, for example. you just want to make sure that a particular object will never be null (for example, to exclude null pointer exceptions). It is because of this βcontractβ that the compiler can optimize if (o! = Null), because it knows that this condition will never happen.
Since they are generally not included at run time, think of them as help in developing a piece of code, rather than as a mechanism for checking errors at run time.
source share