Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assertions

 
Dhanashree Mankar
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
if(i<20)
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
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi dhanashree2004
Read this article on Assertions.
 
Armando Anton
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi:
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic