Boolean objects always true
Brought to you by:
zhourenjian
Because any Boolean instance is an object (not a boolean) when logically evaluated it is always true. The following program try to prove.
public class BooleanBug {
public static void main(String[] args) {
Boolean b = new Boolean(false);
if(!b) {
System.out.println("OK");
}
else {
System.out.println("BUG!");
}
}
}
the correct if expression would be:
if(!b.booleanValue()) {...
I'm not really sure how this can be fixed, perhaps doing special treatment when a Boolean object is evaluated...
It is a bug in the compiler. Compiler should unbox the expression "!b" into "!b.booleanValue()".
"if (b) {}" is compiled correctly into "if (b.booleanValue()) {}".
The bug lies in ASTKeywordVisitor, line 830:
public boolean visit(PrefixExpression node) {
and ASTScriptVisitor, line 1648:
public boolean visit(SimpleName node) {
There should be some lines checking "exp.resolveUnboxing()", similar as in ASTTigerVisitor line 62.