File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertion statements and their compilation

 
Rajeev Trikha
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In K&B ch4/q19 (SCJP 5) there is a statement

As of Java version 1.4, assertion statements are compiled by default.


The answer says it is wrong because:
as of Java 1.4 you must add the argument –source 1.4 to the command line if you want the
compiler to compile assertion statements.


I don't get it. The default behaviour is surely 'javac -source 6' so it should be true. Where am I going wrong?

I just compiled a test program without any -source flag and it works fine. It correctly throws 'assert false' when -ea used. Lost!

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33671
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajeev Trikha wrote:In K&B ch4/q19 (SCJP 5) there is a statement

Typo? Assertions are covered in chapter 5 and the self test goes up to question #16. Or maybe you are using the Java 1.4 version of the book?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33671
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On to the question. Do they mean compiled or enabled? Assertions are always compiled; they are Java code.
 
Rajeev Trikha
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exact question is:

19. Which two of the following statements are true? (Choose two.)

A. If assertions are compiled into a source file, and if no flags are included at runtime,
assertions will execute by default.

B. As of Java version 1.4, assertion statements are compiled by default.

C. With the proper use of runtime arguments, it is possible to instruct the VM to disable assertions for a certain class, and to enable assertions for a certain package, at the same time.

D. The following are all valid runtime assertion flags:
-ea, -esa, -dsa, -enableassertions,
-disablesystemassertions

E. When evaluating command-line arguments, the VM gives –ea flags precedence over –da flags.


The answer is: C and D.

Jeanne, you are right that the latest version doesn't have this question.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic