It's not a secret anymore!
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "assertions" Watch "assertions" New topic


Dhanashree Mankar
Ranch Hand

Joined: Aug 25, 2003
Posts: 123
1> Exactly why we r using assretions?
coz if assertion is as follows
assert i<20:System.out.println("i should be greater than 20");

we can use if clause also as follows
System.out.println("i should be greater than 20");
2> why we cann't use assertions to check perconditions in public methods why only in private methods?
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
Hi dhanashree2004
Read this article on Assertions.
Armando Anton

Joined: Aug 22, 2003
Posts: 16
1. Assertions are used tipically when development to test the application. Using assertions you can active or desactivate them when compiling and at runtime without changing the code. Without assertions you need to change your code and recompile to do/undo the test. Assertions are a great tool to test your application
2. Public methods can be invoked from anywhere (another programmer can use your public methods) and if you want to be sure that your method works correctly you must [B]always[B] check the arguments. If the check is done using assertions you are not sure that the check will be always done because assertions can be desactivated at runtime

Looking for best practices
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
This answer is from Kathy and Bert Book.
"Assertions let you test your assumptions during development, but the asserton code-in effect-evapurate when the program is deployed"
In brief:
When you test your code, you can enable assertion for JDK 1.4, and disable it-defualt- during deployment. In this situation, your code will run faster, stay smaller and cleaner.
If you use "if else" your code will take a performance hit.
I hope this helps

SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
I agree. Here's the link:
subject: assertions
It's not a secret anymore!