File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertions" Watch "Assertions" New topic
Author

Assertions

Saumya Tangeda
Greenhorn

Joined: Jul 09, 2006
Posts: 24
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

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Aum Tao
Ranch Hand

Joined: Feb 14, 2006
Posts: 210
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.


SCJP 1.4 85%
Neelesh Bodas
Ranch Hand

Joined: Jul 20, 2006
Posts: 107
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

Joined: Jul 09, 2006
Posts: 24
Thanks to you all.I understood now the difference between compile-time enabling and runtime enabling of assertions.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Notice for Java 5.0 assertions are, by default, enabled at compile time, but at runtime they are disabled as in 1.4.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assertions