This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
Don't post the question in 2 topics, post the origin of the code (A programmer's guide to Java certification by Khalid Azim Mughal, Rolf W. Rasmussen) and try and read somethings for yourself. Assertions is one of the simplest concepts of java.
Assertion is really simple it is used to check assumptions you make and by default it is assumed your assumption is correct. but when your assumption goes wrong it throws an error and stop executing further codes. it comes in two flavors with one expression and with two expression. in first type the after assert keyword there should be one expression which should always return a boolean value. If it returns true then the execution continues (It is assumed that it will always return true). but if it returns false it throws assertion error. in 2nd case means if it returns false we can use one more expression to know precisely what went wrong with our assumption.
In above program
assert distance>=0.0; means it is assumed that the distance will always be a positive number. if it is not then it will throw an error and stop execution.
assert time>0.0:"Time is not a positive value:"+time; means here also it is assumed that time will always be positive, if it is not then the second expression will be executed and after that execution will stop.
in our case it never happens to be false because supplied value is positive for both time and distance.
Hope it will help a little and please rectify me if i am wrong.
Assertions are used during development and testing to ensure that your assumptions are correct during code execution. If the test fails, you get an AssertionError with either a default message or one you provided in the assert statement itself.
There are guidelines on where and how to use assertions. Briefly these are (use your study guide for more detail):
1) DO NOT use asserts to validate arguments in a public method
2) Do use asserts to validate arguments on a private method
3) DO NOT use asserts to validate command line arguments
4) Do use asserts (even in public methods) to check for conditions that should never occur
5) DO NOT have any asserts that affect execution by changing values etc.
You also need to know when it it legal to use assertions, and that depends on the version of Java you are compiling against (yes, you need to know this for the SCJP6 exam). Prior to version 1.4, "assert" was not a keyword. So, for example, this was legal in Java 1.3And this remains legal with Java 1.6 IF you tell it to compile to version 1.3 (although you will get warnings). If you compile to version 1.4 or higher, compilation will fail (as "assert" is now a keyword).
To use assertions you need to enable them at runtime use "-ea" or "-enableassertions", you can also choose to selectively enable assertions based on package and class names.
Joined: Jul 09, 2009
Hi Jason and Jeetendra
Thanks a lot .I got the concept now very clear now..