File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Assertion in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Assertion in java" Watch "Assertion in java" New topic
Author

Assertion in java

Jairaj Gaur
Greenhorn

Joined: Nov 25, 2010
Posts: 6



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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jairaj Gaur
Greenhorn

Joined: Nov 25, 2010
Posts: 6
Thank You ~ Jesper de Jong

For your Reply!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
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

Joined: Oct 13, 2005
Posts: 37874
    
  22
Try changing m.check(2); to m.check(-99); and see what happens.
Jairaj Gaur
Greenhorn

Joined: Nov 25, 2010
Posts: 6
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

Joined: Oct 13, 2005
Posts: 37874
    
  22
The negative value is supposed to give an error; your assertion is failing. Try changing the line to
Jairaj Gaur
Greenhorn

Joined: Nov 25, 2010
Posts: 6
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jairaj Gaur
Greenhorn

Joined: Nov 25, 2010
Posts: 6
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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

Joined: Nov 25, 2010
Posts: 6
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assertion in java
 
Similar Threads
Assertion enable or not?
Mock Questions
Assertion doubt
Question on shift operator
Following is a question