views:

230

answers:

4

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!)

A: 

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.

jjnguy
add a -ea to the runtime of the VM
Michael Wiles
Bah.... accepted before anyone gets a chance to answer.
skaffman
2 People got a chance to answer...
jjnguy
yeah, in the 2 minutes the question was open for...
skaffman
10 minutes .
jjnguy
Why Eclipse? Is the same using command line! and probably the default for Netbeans...
Carlos Heuberger
The reason I specified Eclipse is because I tend to see people using Eclipse have this problem more often than other people.
jjnguy
+4  A: 

When running the program, you have to enable assertions in the Java VM by adding '-ea' to the command line:

java -ea -jar myprogram.jar
Michael Donohue
Thanks guys! Got it working as it should! cheers for the super fast replies!
+1  A: 

Java-language assertions are weird. You have to enable them when you launch the command line, and I don't like that.

For this reason, I tend to use 3rd-party libraries to do my assertions. Apache Commons Lang (via the Validator class), Spring (Via the Assert class) or even JUnit4 (via the Assert class) all provide this functionality, and it'll work regardless of VM settings. When you use Java5 static imports, they're just as easy to use as java assert, plus they're more flexible, plus they allow you to specify the error message in the exception.

skaffman
The default is annoying, but is there for backward compatibility; For the other things: you can specify the message with java assertions as well, and they are guarantied afaik to be removed from the byte code when they are not used. This means you don't have to worry about production performance so much. I doubt this would be possible with a third party library
Jens Schauder
True, but performance penalty for checking these things is completely insignificant on a modern VM.
skaffman
You can put heavy weight assertions in with the language assertions, and know that they will be removed in a production deployment. Assertions are not removed from the bytecode, the VM just compiles them away at runtime depending on the flags.
Michael Donohue
A: 

This makes the information if Assertions are enabled accessible in the program.

If assertions are disabled (this is the default) the assertion statement will not be executet, and mybool will have the value false.

if assertions are enabled (jvm argument -ea) the assertion will get executed, and by a side effect mybool will be set to true.

You can use that to enforce enabling or disabling of assertions. For example I have a Test in my TestSuites that failes if assertions are not enabled, in order to enforce that the assertions are always on when running the tests.

Jens Schauder