aspose 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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertions" Watch "Assertions" New topic
Author

Assertions

Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75
Can someone explain what is an Assertion
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Java assertion
What don't you get?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75



In the above code what does assert statement do ....???
In order to see how does assert statment exhibit its functianality is something that i dont understand???
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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.

Wouter Oet
jeetendra Choudhary
Ranch Hand

Joined: Jun 02, 2009
Posts: 33
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.

Thanks & Regards
Jeetendra..!!


Preparing for SCJP...!!
Jason Irwin
Ranch Hand

Joined: Jun 09, 2009
Posts: 327
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.


SCJP6
Sonali Sehgal
Ranch Hand

Joined: Jul 09, 2009
Posts: 75
Hi Jason and Jeetendra

Thanks a lot .I got the concept now very clear now..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assertions