This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertions" Watch "Assertions" New topic
Author

Assertions

Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Can somebody please explain to me the follwoing staement:

"Assertions can be enabled or disabled at runtime, depending on whether you are debugging your application or not. They are typically turned on at testing and turned off at implementation time."

Thanks.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Loveleen,

Assertion is good utility for prgrammers to get away from doing exception handling at everytime, even though not required while testing or debugging the code; Suppose you have to check some variable's value should never be zero so you could write as
assert a!=0;

By the way you may be using this "a" to divide any number, so simply you asserted it, instead of just putting the code inside try catch block, unnecessarily. You want to be 100 % sure that it wont be zero. Therefore the assert provides you save the number of lines also, otherwise you could have made if else or something for check.

Yeah, when development and testing has been done you need not to enable the assertion, because its purpose has been availed while testing was going on. Thankfuly assertion is disabled at runtime as its default setting. You explicitely enable it using -enableassertion or -ea while launching your program.

Simple using assert while testing the code saves undesirable or say it lengthy exception handling everytime something has to be tested. But beware exception handling has its own important role, assertion does not replace exception handling though.
I think you got the meaning of sentence.

cmbhatt


cmbhatt
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Thanks Chandra for the explanation.
You seem to be well conversed in Java. Just curious, Are you a programmer?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Guptajee,

Thanks for compliments.
You can think me like you, who is preparing for exam.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Assertions
 
Similar Threads
assertions
java assertions
assertion code overhead when disabled
Assertions
why use assertion rather than xception Handling