• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertion in java

 
Jairaj Gaur
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Hello Frds,

In this program I used asset keyword..
I just want to know what is the difference between simple & really simple asset
& wat ll be the output for this program.
& if output is "Java==================" then wat does "assert(a>0):"a is"+a;" this line means & what is the use of it.

Waiting for your reply!!!

From ~ Jai
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15459
42
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Please UseCodeTags when you post source code.

Assertions are used to check if values, for examples of arguments to a method, are correct. They are really meant as a debugging tool. If the expression used in the assert statement evaluates to false, an AssertionError will be thrown. That makes it easy to detect when a wrong value is passed to a method, to help you find bugs in the program.

For detailed information, see: Programming With Assertions.
 
Jairaj Gaur
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You ~ Jesper de Jong

For your Reply!!!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means you have written a class invariant requiring that a be > 0 ; at that point you use theassert keyword to check whether your class invariant has been breached. You would execute that class with
java -ea Money
Then you can see whether an error has occurred.

Since you are new, I shall see if I can add code tags to your post, and you can see how much better it looks, if you have correct indentation.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try changing m.check(2); to m.check(-99); and see what happens.
 
Jairaj Gaur
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Try changing m.check(2); to m.check(-99); and see what happens.



I tried both +ive & -ive value. In +ive value case ,output was "Java ==========", But at -ive value it is giving error. Still my question is nt solved.

What the use of :"a is"+a;

when this condition will execute?
assert(a>0):"a is"+a;
 
Campbell Ritchie
Sheriff
Pie
Posts: 49865
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The negative value is supposed to give an error; your assertion is failing. Try changing the line to
 
Jairaj Gaur
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The negative value is supposed to give an error; your assertion is failing. Try changing the line to



Thank You ~ Campbell Ritchie

This is what I want to know.
 
Rob Spoor
Sheriff
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that you should never ever ever use assertions to validate input to non-private methods, as you don't fully control who is calling those methods. If assertions are the only validation then that validation can be turned off by simply turning off assertions. So instead of
you should write
For private methods or code points inside methods where input validation already took place, assertions are just fine.
 
Jairaj Gaur
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Note that you should never ever ever use assertions to validate input to non-private methods, as you don't fully control who is calling those methods. If assertions are the only validation then that validation can be turned off by simply turning off assertions. So instead of
you should write
For private methods or code points inside methods where input validation already took place, assertions are just fine.



What you mean by turning ON &turning OFF of assertions & how do we can achieve it..
 
Rob Spoor
Sheriff
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Turn them on: use the -ea JVM flag.
Turn them off: don't use that flag.

That's the main thing about assertions - they are conditionally enabled. You should never use any logic that relies on the fact that they are turned on or off. That also means that the assertions cannot have any side effects. The following is just wrong:
 
Jairaj Gaur
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Turn them on: use the -ea JVM flag.
Turn them off: don't use that flag.

That's the main thing about assertions - they are conditionally enabled. You should never use any logic that relies on the fact that they are turned on or off. That also means that the assertions cannot have any side effects. The following is just wrong:




Thank You ~ Rob Prime
 
Rob Spoor
Sheriff
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic