Code not producing desired result.

J

J. Davidson

Lew said:
Because it would never evaluate the right-hand side once the left-hand
side went 'true', and is therefore a very silly operator?


| and & aren't logical operators on int types, they're arithmetic
operators, ergo not for logic at all. They are logical operators when
applied to boolean operands.

The term I learned for such operations is "bitwise logic", with
"arithmetic" referring to ordinary addition, multiplication, and the like.
||, &&, | and & are all "normally" used for boolean operations on
booleans.

My experience is that I hardly ever use, or even see, single | and &
used on booleans. This is what I meant in excluding them from my list of
what's "normally" used; normal as in what is statistically typical.

Apparently either your experience differs or you meant "normal" in the
sense of "permitted by the language specification".

- jenny
 
J

J. Davidson

Tom said:
That's exactly why you'd want it:

isDatabaseInitialised ||= initialiseDatabase() ;

Or just

isFoo ||= computeAndReturnSomeExpensiveToComputeBoolean();

or

isFoo ||= somethingThatWillBlowUpIfAlreadyFoo();

(say, isFoo will already be true if some other condition is true, such
as that something is null, that had darn well better be false if that
method is called)

Anywhere where isFoo = isFoo || bar wins over isFoo = isFoo | bar, ||=
could conceivably be useful.

- jenny
 
J

John B. Matthews

"J. Davidson said:
The term I learned for such operations is "bitwise logic", with
"arithmetic" referring to ordinary addition, multiplication, and
the like.

These issues are difficult to discuss without common terminology. For
this reason, Lew helpfully referred us to the relevant sections of the
JLS:

<http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.22>

In section 15.22, it is clear that Bitwise and Logical Operators (&, |,
^) may be applied to numeric or boolean operands. As these operators
may be used to perform certain set or modular operations on numeric
arguments, the term "arithmetic operators" seems apt. As the operators
may also be applied to operands of type boolean, the term "logical
operators" makes sense in that context.

This article expands on the topic of bitwise operators in both arithmetic
and logical contexts:


In sections 15.23 and 15.24, it is clear that the Conditional operators
(&&, ||) apply only to results of type boolean and include a convenient
"short-circuit" feature, although that term is not used. Certainly the
conditional operators afford some efficiency, but one must be carful
not to optimize away the result.
My experience is that I hardly ever use, or even see, single | and &
used on booleans. This is what I meant in excluding them from my list of
what's "normally" used; normal as in what is statistically typical.

This is not my experience. You have adduced examples where the
conditional operator was optimal; similarly, a logical operator may be
correct when side-effects of evaluation are intended. Recalling that
most programming errors are logic errors, I would advocate using
whichever operator specifies the desired operation.
Apparently either your experience differs or you meant "normal" in the
sense of "permitted by the language specification".

I might have said "common", "usual," or "customary".
 
A

Andreas Leitgeb

Lew said:
Because it would never evaluate the right-hand side once the left-hand side
went 'true',

Yes, that would perfectly well describe it, and be exactly
the use of it.
and is therefore a very silly operator?

It's not at all a "silly" operator.

<howitwouldbe>
boolean ok=tryPlanA(...);
ok ||= tryPlanB(...);
ok ||= tryPlanC(...);
ok ||= tryPlanD(...);
if (ok) { /* successful after all */ }
</howitwouldbe>

One might argue it doesn't fit into Java, and I'd
even agree, since rewriting this idiom in legal Java
is quite easy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top