Search code examples
javabooleanassertions

java assert gives strange results


what should the following java code do?

public class foo{
    public static void main(String[] args){
        boolean mybool=false;
        assert (mybool==true);
    }
}

Should this throw an assertion error? and if not why not? (I'm not getting any errors!)


Solution

  • This should be throwing AssertionErors.

    You need to turn on assertions if you are using Eclipse. It has them disabled by default.

    To do this, add -ea to the JVM arguments.