wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes not able to understand postcondition in assertions 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "not able to understand postcondition in assertions" Watch "not able to understand postcondition in assertions" New topic
Author

not able to understand postcondition in assertions

shreya prabhu
Ranch Hand

Joined: Feb 10, 2007
Posts: 31
public class AssertTest{
public void writeFile(FileOutputStrem fout){
assert fout!=null:"Invalid Handle file";
try{
fout.write(1);
fout.close();
}catch(IOException ignore){

}
}//invalid use of aasertions should not be used to check validity of public arguments


public assertDemo{
public String getLanguage(String code){
String str=null;
assert str!=null;
return str;
}
} //irrespective of visibility it is valid to use assertions tocheck for postconditions

not able to understand what exactly is the difference.please help to clear my doubt.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I'm not sure, but I think you're asking about the optional expression in an assertion. That is, the difference between...

assert boolean;

...and...

assert boolean: expression;

If an expression is included, then it is passed to the constructor of the AssertionError, where it's converted to a String message. Basically, it allows you to pass some additional information to the Error.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
shreya prabhu
Ranch Hand

Joined: Feb 10, 2007
Posts: 31
actually above 2 are examples from whizlabs mock exams.why assertion in the first program valid and not in the second one?i thought it is invalid to use assertions to check arguments in public method.but not able to find out why then is the second condition valid?
ShivKumar Rajawat
Greenhorn

Joined: Mar 13, 2007
Posts: 17
I do not really understand what is meant by postconditions. But yes Archna you are right about arguments to public methods.

In the second example the argument is "String code" . and we are not asserting on "code" that is why this rule can not be applied in the 2nd example.


Regards<br />-Shiv
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by ShivKumar Rajawat:
...In the second example the argument is "String code" . and we are not asserting on "code" ...

Rafal Grzybacz
Greenhorn

Joined: Aug 19, 2006
Posts: 6
The use of assertion in 1st code is invalid because it is checking the parameter of a public method.

The second code demonstrates a valid/proper way of using assertions. It checks if the reference to a String object is not null.

Postcondition means that the assertion checks the condition of something after it executes.

Hope this helps.

Rafal.
m Sabale
Greenhorn

Joined: Mar 22, 2005
Posts: 5
Hi
If you are not clear about assertion preconditions and postconditions, go thru the sun tutorial

http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html

ther is a clear explaination with easy examples
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: not able to understand postcondition in assertions
 
Similar Threads
Doubt in try and finally block execution.
How to enable Assertions
Assertions
Public method argument validation
assertions