• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

not able to understand postcondition in assertions

 
shreya prabhu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
shreya prabhu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ShivKumar Rajawat:
...In the second example the argument is "String code" . and we are not asserting on "code" ...

 
Rafal Grzybacz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic