Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertions

 
Saumya Tangeda
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read the full story in Programming for Assertions

For 3: Throwing an AssertionError is completely independent of the status of assertion enabling (compile time or runtime). You can use it without using an assert statement in your program.
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enabling assertions while compiling and enabling assertions while running a java file have very different results.

Using -source 1.4 enables you to use assert as a keyword. It does not enable assertions at runtime. For this, we use the -ea and -da at the command prompt while running the java program.

You are mixing the two concepts.
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Saumya Tangeda
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to you all.I understood now the difference between compile-time enabling and runtime enabling of assertions.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice for Java 5.0 assertions are, by default, enabled at compile time, but at runtime they are disabled as in 1.4.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic