views:

371

answers:

8

(a && b) has the same value as (a & b), but with && the expression b is not executed if a is false. In Java there is also an operator &=. a &= b is the same as a = a & b.

Question: Why isn't there an operator &&=, at least in Java. Is there some reason why I would not make sense to have it, or is it simply that no one cares or needs it?

+4  A: 

It is this way in Java, because it is this way in C.

Now the question why it is so in C is because when & and && became different operators (sometime preceding C's descent from B), the &= variety of operators was simply overlooked.

But the second part of my answer does not have any sources to back it up.

EFraim
+9  A: 

&& is a logical operator, & is a bitwise operator. They do two different things. Assigning the results of the former to the left side of the operation makes sense less often than for bitwise operations.

Alex Barrett
Now, I've seen a lot of situation where it does make sense:for instance: bool failed = operation1(); failed failed
EFraim
Tom Hawtin - tackline
@Tom: Yes, it does, but the form I've posted would short-circuit and nothing would be done after the first failure. Convenient.
EFraim
Daniel Brückner
Alex Barrett
@Alex: just FYI I have not down-voted you.
EFraim
that is, the result is true if and only if both its operands are true.' http://msdn.microsoft.com/en-us/library/sbf85k1c(VS.71).aspx
Daniel Brückner
That makes a lot of sense for `bool` types, as the two operators are functionally equivalent.And no problem EFraim, apparently two other people have found it inaccurate enough to do so though. Perhaps I should remove the word "extremely" :p
Alex Barrett
Pavel Minaev
Yishai
I am open to suggestions to improving the terminology. I am not a Java programmer, so it does not surprise me to learn that my answer does not cover all cases.
Alex Barrett
+1  A: 

It is allowed in Ruby.

If I were to guess, I would say that it is not frequently used so it wasn't implemented. Another explanation could be that the parser only looks at the character before the =

Zaid Zawaideh
Java supports <<=, >>= and >>>=, so that isn't strictly true.
Yishai
true. I didn't think of those. I guess the only explanation is then how frequently it is used.
Zaid Zawaideh
A: 

a&b and a && b are not the same thing.

a && b is a boolean expression that returns a boolean, and a & b is a bitwise expression that returns an int (if a and b are ints).

whare do you think they are the same?

Omry
Tom Hawtin - tackline
Daniel Brückner
+4  A: 

I cannot think of any better reason then 'It looks incredible ugly!'

Daniel Brückner
But then C/Java was never meant to be beautiful.
EFraim
+3  A: 

If I understand the logic of

a = a && b

b only gets evaluated if a is true. So...

a &&= b

If a were already false nothing would happen? b would never be evaluated right?

Maybe it's just clearer to write

if (a)
    a = b;
n8wrl
this is a question, not an answer :P
klez
True - just thinking it thru. Seems like an odd, difficult-to-read form of if (!a) a=b
n8wrl
Best comment for a long time ... :D
Daniel Brückner
Daniel Brückner
Yeah but it makes the short-circuiting obvious
n8wrl
+1  A: 

'&' and '&&' are not the same as '&&' is a short cut operation which will not do if the first operand is false while '&' will do it anyway (works with both number and boolean).

I do agree that it make more sense to exist but it is not that bad if it is not there. I guess it was not there because C does not have it.

Really can't think of why.

NawaMan
+2  A: 

One of Java's original aims was to be "Simple, Object Oriented, and Familiar." As applied to this case, &= is familiar (C, C++ have it and familiar in this context meant familiar to someone who knows those two).

&&= would not be familiar, and it would not be simple, in the sense that the language designers were not looking to think of every operator they could add to the language, so less extra operators are simpler.

Yishai