• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

not able to understand postcondition in assertions

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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" ...


 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic