Java assert - what I learnt today

noreply@blogger.com (Gareth) Published: February 11, 2009
I learnt something new today, the following I thought would be just fine:

File f = new File("foo.txt");

if( f.exists() )
assert f.delete();

That all looked good to me, right up to the point that I discovered that an assert expression isn't even evaluated if asserts are not enabled, had to change it to.

if( f.exists() ){
boolean deleted = f.delete();
assert deleted;
}