Subj : Re: Rhino: !(java.lang.Boolean.FALSE) != !false To : Attila Szegedi From : Igor Bukanov Date : Fri Oct 29 2004 12:14 pm Attila Szegedi wrote: > Hi folks, > > Here's a shell session I just run: > > C:\projects\mozilla\mozilla\js\rhino\build\rhino1_6R1pre>java -jar js.jar > Rhino 1.6 release 1_pre_RC3 2004 10 27 > js> java.lang.Boolean.FALSE == false > false > js> > > Now, I understand that this slightly unintuitive result is due to the > fact that the expresisons gets evaluated (if I interpreted correctly) > according to the point 19 of "The Abstract Equality Comparison > Algorithm" (ECMA-262 section 11.9.3). The section 19: 19. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y). Now Rhino does not define any special conversion from java.lang.Boolean to JS number so to fix this you can file an enhancement report at bugzilla.mozilla.org. But note that due to discrepancies of ECMAScript conversion to primitive boolean cases like: if (java.lang.Boolean.FALSE) part_a; else part_b; or even if (new Boolean(false)) part_a; else part_b; would evaluate part_a, not part_b: see bellow. > However, I even attempted forcing > the boolean > comparison, as hinted on page 56 of ECMA-262: > > js> !java.lang.Boolean.FALSE == !false > false > > then, thinking maybe associativity is the problem: > > js> !(java.lang.Boolean.FALSE) == !false > false > > Now, I'd expect at least this last expression to work, since > NativeJavaObject.defaultValue(ScriptRuntime.BooleanClass) ought to > correctly convert > it into a boolean. Am I overlooking something? Yes: ECMA 252 requires that !x should first convert x to a primitive boolean value and then apply "!". Now the table in 9.2 "ToBoolean" states that ToBoolean() applied to object should always give true thus !java.lang.Boolean.FALSE is equivalent to !true resulting in false. Note this also leads to completely unintuitive but required by standard behavior with boxed booleans in JS: Boolean(new Boolean(false)) == false => false !new Boolean(false) == !false => false while new Boolean(false) == false => true. Regards, Igor .