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 Understanding assertion mechanism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Understanding assertion mechanism" Watch "Understanding assertion mechanism" New topic
Author

Understanding assertion mechanism

Mckenzie John
Greenhorn

Joined: Sep 21, 2012
Posts: 16
Hi ,

I have a doubt regarding the assertion mechanism. Let me explain:

// code
assert( value >= 0 && value <= 20 ) : "Invalid number: " + value;
// coding continues

In the above scenario,

1) when is the assertionError thrown? when the condition is false - Is the value is printed as similar to an SOP along with the stackTrace?

2) If the condition is true , say the relational operator returns true , the coding continues without printing the value . Is my understanding correct?

3) In SCJP real exam, what answer must i choose if i get a question like the above is asked - what is the output of an assert statement - without mentioning anything about it being enabled or disabled? I know that assertions are disabled by default. So should i assume that even though assertion is present it has no use as assertion is not enabled and mark the choice that the coding proceeds normally?


Thanks in Advance.

Regards,
Mck
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

your first and second statements are correct. And you can run your program to see the exact stacktrace.
In regard to the third statement I think you should consider that the assertion is enabled however I would like other ranchers to back my statement.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Mckenzie John wrote:
1) when is the assertionError thrown? when the condition is false - Is the value is printed as similar to an SOP along with the stackTrace?


the above one is interpreted by compiler as in

So, that means you are passing the error message to AssertionError's constructor . you can get the message using getMessage() in future.

2) If the condition is true , say the relational operator returns true ,
the coding continues without printing the value . Is my understanding correct?

Yes, with out throwing an Error.

3) In SCJP real exam, what answer must i choose if i get a question like the above is asked - what is the output of an assert statement - without mentioning anything about it being enabled or disabled? I know that assertions are disabled by default. So should i assume that even though assertion is present it has no use as assertion is not enabled and mark the choice that the coding proceeds normally?


Here I am not sure. hope, some other rancher can help you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding assertion mechanism
 
Similar Threads
Assertion
dan exam doubt 12
Regarding to assertions
Assertion Question
Assertion