• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Assertions

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone explain what is an Assertion
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java assertion
What don't you get?
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 41
Mac OS X Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..!!
 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jason and Jeetendra

Thanks a lot .I got the concept now very clear now..
 
this is supposed to be a surprise, but it smells like a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic