Possible Duplicates:
Why doesn't Java have compound assignment versions of the conditional-and and conditional-or operators? (&&=, ||=)
Why does a “&&=” Operator not exist?
Today at work I wrote the following LOC (the real identities of b and b1 are confidential :)
b &&= b1; // meaning b = b && b1;
I stared at it for a couple of seconds and realized that there exists no such operator. Just to be sure, I clicked on compile and it failed. To be dead sure I consulted the standard.
Are there specific reasons that there are no such operators? I can think of some:
b &&= b1
and b = b && b1
may not be equivalent because of short-circuit evaluation of &&.I do not claim that it would be very useful to have such operators, no. I also don't claim that any or all of the three above reasons are not enough to refrain from creating that operator. My question is the following: is there maybe a much more serious reason which I am overseeing?
I don't know why both the question and some of the answers mention short-circuiting behavior of the corresponding logical operators as a potential issue.
There's absolutely no short-circuit-related problems with defining &&=
and ||=
operators. They should be defined uniformly with +=
and other similar operators, meaning that a &&= b
should be equivalent to a = a && b
, but with a
being evaluated only once in &&=
version. This means in turn that b
is not evaluated at all if a
is originally zero. Easy.
So, the only reason they don't exist in the language is, well, "just because".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With