According to the assertions topic in K&B, once we use the -source 1.4 during compile time assertions are enabled and assert is used as a keyword not an identifier. 1. I want to know why do we have the -ea and -da commands if we can enable assertions using -source 1.4 and disable using -source1.3?Other than to enable/disable assertions for a specific class/package, i don't see any use of these commands. 2.what does the -ea command do when assertions are already enabled using -source 1.4 command at compile time? 3. what happens when we use 'throw new AssertionError()' when assertions are enabled and when they are disabled? [ July 20, 2006: Message edited by: Barry Gaunt ]
The -source flag is for javac (The java compiler which converts the java source into the bytecode) whereas the -ea and -da flags are for the java interpreter. These two have very different semantics : The -source flag says whether "assert" should be treated as a keyword or not. If java program is compiled with -source=1.3 or lower, assert will not be treated as a keyword since java 1.3 and lower didnot have that keyword.
The -da or -ea flags indicate whether the assertions will "actually be active" at the runtime. They donot make sense if you have compiled the code with -source=1.3 or lower, but if you have compiled with -source=1.4 or above, the assertions will be thrown at runtime only if the -ea option is provided with java command (and of course, only if the assert condition evaluates to false).
Joined: Jul 09, 2006
Thanks to you all.I understood now the difference between compile-time enabling and runtime enabling of assertions.
Joined: Aug 03, 2002
Notice for Java 5.0 assertions are, by default, enabled at compile time, but at runtime they are disabled as in 1.4.