Re: =?windows-1252?Q?Java's_Broken_Booleans?=
On 02/15/2011 08:05 AM, Andreas Leitgeb wrote:
Andreas Leitgeb<avl@gamma.logic.tuwien.ac.at> wrote:
I'm quite comfortable with Java's restrictive way for conversion
towards boolean, but a bit unhappy about the other direction -
maybe this feeling was triggered by the truely moronic looking
byte code generated for the "bool?1:0" idiom.
Btw., in the bytecode, there's nothing to identify a particular
stack-position as having been a boolean, so hotspot doesn't
have the necessary information available to know that:
11: iload_1
12: ifeq 19
15: iconst_1
16: goto 20
19: iconst_0
20: istore_2
is really guaranteed to be equivalent to iload_1,istore_2
Well, to be fair, there is nothing in the JVM that actually guarantees
that true == 1; the typical connotation is that nonzero is true
(extrapolating from C), so this is "safer" than just converting them and
hoping that your true value is actually 1. In any case, I suspect the
JIT would rip all those checks out, so it probably doesn't matter all
that much in terms of performance...
Incidentally, this discussion recalled me of a point noted in another
project I worked on, where people discovered bugs as a result of values
other than 0 and 1 being placed in a boolean variable.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth