aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertion" Watch "Assertion" New topic
Author

Assertion

agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
SUN Tutorial Says That
the method guarantees that it will always enforce the argument checks. It must check its arguments whether or not assertions are enabled. Further, the assert construct does not throw an exception of the specified type. It can throw only an AssertionError.
The Above Mentioned Statement Should Be Applied Both For public as Well AS private Methos Because Both Of Them Check Their Arguments But assertion Can't Be Applied For Checking Public Method's argument

Please Explain Why?
**********************
Agrah
[ September 23, 2005: Message edited by: agrah upadhyay ]
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

http://faq.javaranch.com/view?SearchFirst

Programming With Assertions

Putting Assertions Into Your Code"
Argument checking is typically part of the published specifications (or contract) of a method, and these specifications must be obeyed whether assertions are enabled or disabled. Another problem with using assertions for argument checking is that erroneous arguments should result in an appropriate runtime exception (such as IllegalArgumentException, IndexOutOfBoundsException, or NullPointerException). An assertion failure will not throw an appropriate exception.
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
Hello Mr.Steve,
I've Gone Through The SUN Official Link For It And Still I Am Not Able To Understand It.And Only This Point.If You Don't Know Answer Simply Don't Answer.......
What Is The Meaning Of Posting A Quote From The Same Site Whose Reference And Qoute Has Been Given By Me !
###############################################################
Thanx
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

If You Don't Know Answer Simply Don't Answer.......
I know the answer. If you don't understand it, you need to explain what it is about the concept you don't understand. Your question was this: "But assertion Can't Be Applied For Checking Public Method's argument...Please Explain Why?". The explanation as to why is contained in the material I posted. If you're still confused by it, explain what it is you don't understand.

What Is The Meaning Of Posting A Quote From The Same Site Whose Reference And Qoute Has Been Given By Me !
First, I have no way of knowing you read the quote I posted from the tutorial. Second, the quote I posted elaborates on and explains the quote you posted. What is it that you don't understand about it?

Seriously, if you can't be bothered to explain or clarify your confusion, you should consider not asking any more questions. You should also try to keep your animus from spilling over into every thread.
[ September 23, 2005: Message edited by: Barry Gaunt ]
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
ONE MORE THING
******************************************************************

Requiring that Assertions are Enabled
Programmers of certain critical systems might wish to ensure that assertions are not disabled in the field. The following static initialization idiom prevents a class from being initialized if its assertions have been disabled:

static {
boolean assertsEnabled = false;
assert assertsEnabled = true; // Intentional side effect!!!
if (!assertsEnabled)
throw new RuntimeException("Asserts must be enabled!!!");
}
Put this static-initializer at the top of your class.
*****************************************************************
The Above Has Beeen Given In SUN 's Official Site.I Want To Ask In Third Line Which Is:
assert assertsEnabled = true;
IN Above [assertsEnabled = true] is Not Evaluating To true Why?

********************************************
Thanx
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
quote
********
Seriously, if you can't be bothered to explain or clarify your confusion, you should consider not asking any more questions
Hey Mr. Anderson Who Are You To Say .......I Am Not A Member Of This Forum Due To You.And The Question May Be Nonsense To You So Better Don't Answer.
And Like Tomorrow Don't Make It Issue.Today Itself You Convinced Me That You Won't Post Answer To My Questions Any More So I Took A Sigh Of Relief.
Please You Are Requsted To Maintain Specifications Of This Forum And Again
If You Don't Know The Answer Please Don't Beat Around The Bush.
***************************************
Thanx
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

See if this helps your understanding...

As stated in the tutorial, the public API describes a contract by which clients interact with objects in the API. That contract should include handling any required validation whether or not assertions are "turned on". Furthermore, the argument validation should/will throw the appropriate exception type (e.g., IllegalArgumentException, NullPointerException, etc.). Assertions can only throw AssertionError, which is not typically appropriate for production/public code.

Hope this helps, and good luck.
[ September 23, 2005: Message edited by: Steve Morrow ]
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Hey Mr. Anderson Who Are You To Say .......
I Am Not A Member Of This Forum Due To You.And The Question May Be Nonsense To You So Better Don't Answer.

I really don't know what your problem is. I provided you a clear, concise answer to your question.

You really need to familiarize yourself with the following:
http://faq.javaranch.com/view?HowToAskQuestionsOnJavaRanch

We're all expected to abide by the "specifications" of this forum.

If You Don't Know The Answer Please Don't Beat Around The Bush.

Okay, yesterday you took offense when I asked you to clarify your confusion. Today, you take offense when I provide you a direct answer. What do expect to obtain from this forum? Why are you taking offense at every answer I try to help you with?
[ September 23, 2005: Message edited by: Steve Morrow ]
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

IN Above [assertsEnabled = true] is Not Evaluating To true Why?

It does evaluate to true if assertions are enabled.

If assertions are enabled, the marked line will assign true to assertsEnabled, pass the assertion, and not make it into the if block. When assertions are disabled, the marked line is ignored and since assertEnabled remains false, the code throws a RuntimeException.

If this is not clear, don't talk back or insult; just explain what it is you're still confused about.
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
But Mr.Steve The Statement Following assert Should evaluate To True.....but Here It Is An Assignment Only.Why?
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

But Mr.Steve The Statement Following assert Should evaluate To True.....but Here It Is An Assignment Only.Why?
Boolean assignment expressions evaluate to the right-hand value (i.e., the expression "assertsEnabled = true" evaluates to true). If assertions are enabled, not only will the expression pass the assertion, but it will also assign true to the variable. This will cause the if() condition to fail, and no RuntimeException will be thrown. It's a simple way of making sure that assertions are enabled.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I've been doing a little bit of post chopping, I hope you all understand. I'll get back to this topic when I get home tonight.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
agrah and vaidehi lalitha,

Sorry for intrrupting you people in your routine Fights.
First of All, Agrah let me clear you one point... Sometimes Something can be taught and understood by a learner very easily if instead of answring his questions directly, a question is put in front of him.

I read you last topic also yesterday...
and I am sorry to say...YOU misunderstood Steve's intentions. Yesterday he asked you a question about your confusion and you felt insulted. Today he gave you a full Explination of what you need to understand..and again you felt insulted. I think this is your problem and you should be polite with your words ..this is an advice to you.

And vaidehi lalitha, Mind you one thing you are no one to ask someone to stop answering his or her questions. Infact as i can see you have said to Steve that "You don't seem To answer it in efficient manner." in your VERY FIRST POST.
May i know how long have you been the member of this forum. and how did u judged that Steve is not answering well.

I think Agrah you should reconsider your way of postings. this is an open forum for every member..you should learn how to go along with other members without hurting their feelings..
[ September 23, 2005: Message edited by: Sandeep Chhabra ]

Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
Right Mr.Sandeep I Will try To Follow You.
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Originally posted by agrah upadhyay:
SUN Tutorial Says That
the method guarantees that it will always enforce the argument checks. It must check its arguments whether or not assertions are enabled. Further, the assert construct does not throw an exception of the specified type. It can throw only an AssertionError.
The Above Mentioned Statement Should Be Applied Both For public as Well AS private Methos Because Both Of Them Check Their Arguments But assertion Can't Be Applied For Checking Public Method's argument

Please Explain Why?
**********************
Agrah

[ September 23, 2005: Message edited by: agrah upadhyay ]



For the statement :"But assertion Can't Be Applied For Checking Public Method's argument"

I dont think it is true. Infact you could easily use assertions to check public method's arguements. But The Point is :
Assertions Should not be applied to public Method's Arguments.
and you can find the reason for this in the link that steve has provided..Read that carefully again.
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Originally posted by agrah upadhyay:
ONE MORE THING
******************************************************************

Requiring that Assertions are Enabled
Programmers of certain critical systems might wish to ensure that assertions are not disabled in the field. The following static initialization idiom prevents a class from being initialized if its assertions have been disabled:

static {
boolean assertsEnabled = false;
assert assertsEnabled = true; // Intentional side effect!!!
if (!assertsEnabled)
throw new RuntimeException("Asserts must be enabled!!!");
}
Put this static-initializer at the top of your class.
*****************************************************************
The Above Has Beeen Given In SUN 's Official Site.I Want To Ask In Third Line Which Is:
assert assertsEnabled = true;
IN Above [assertsEnabled = true] is Not Evaluating To true Why?

********************************************
Thanx


IN Above [assertsEnabled = true] is Not Evaluating To true Why?

This line would surely evaluate to true IF THE ASSERTIONS ARE ENABLED WHILE RUNNING THE PROGRAM (ie -ea or -enableassertions flag is provided)
In case assertions are not enabled then the program will terimnate throwing a runtime exception Asserts must be enabled!!!".
And this is what we wanted if assertions are not enabled the program should not run.

Hope this thing will clearify some points.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assertion