• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

assertions

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi dhanashree2004
Read this article on Assertions.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic