tags:

views:

98

answers:

3

Assuming

boolean a = false;

I was wondering if doing:

a &= b; 

is equivalent to

a = a && b; //logical AND, a is false hence b is not evaluated.

or on the other hand it means

a = a & b; //Bitwise AND. Both a and b are evaluated.

Thanks in advance.

+12  A: 

It's the last one:

a = a & b;
Philippe Leybaert
+5  A: 

From the Java Language Specification - 15.26.2 Compound Assignment Operators.

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

So a &= b; is equivalent to a = a & b;

Stephen C
+1  A: 

see 15.22.2 of the jsl. For boolean operands, the & operator is boolean, not bitwise. The only difference between && and & for boolean operands is that for && it is short circuited (meaning that the second operand isn't evaluated if the first operand evaluates to false).

So in your case, if b is a primitive, a = a && b, a = a & b, and a &= b all do the same thing.

stew