1

Sorry, for the strange question formulation. If somebody has an idea how to make it better, I will be happy. Lest imagine we have 3 boolean variable:

boolean a = false;
boolean b = false;
boolean c = false;

Java allows to write the folowing:

System.out.println(a=b);
System.out.println(a==b & a==c);

And from this two statements I expected that the following is legal, too.

System.out.println(a=b & a=c);

My question is: why in the second case it isn't allowed, when it is allowed in the first one? In the second case both assignments resolved in boolean and & looks legal for me.

serg
  • 1,003
  • 3
  • 16
  • 26
  • Ain't you missing `&&` --- an ampersand symbol! That's a bitwise and which has lower priority as mentioned in the answer. – Am_I_Helpful Dec 01 '14 at 17:21
  • 1
    @shekharsuman Java _does_ have a `&` boolean operator; it is like `&&` except that it does not short circuit – fge Dec 01 '14 at 17:22
  • @fge-That I mentioned in my comment too! You should check it,and,I wanted to say that it'll get short-circuited!!! – Am_I_Helpful Dec 01 '14 at 17:23
  • @shekharsuman sorry, reacted too fast; anyway, I believe the OP knows about this operator from the look of things – fge Dec 01 '14 at 17:27
  • @fge one question touching this. I have not come across in my career where i need to use bitwise operator. Can you give some example where it may be required in practical scenario? – M Sach Dec 01 '14 at 17:47
  • 2
    @MSach if you have to manipulate flags based on numeric values; for instance, [`Spliterator.characteristics()`](http://docs.oracle.com/javase/8/docs/api/java/util/Spliterator.html#characteristics--). If you want to test, say, whether a spliterator has the `DISTINCT` characteristics, you'll test that `theSpliterator.characteristics() & DISTINCT == DISTINCT` (or `!= 0`). Granted, in Java you won't need it that much, but it is quite common in C. Another way is to get the next closest multiple of `n` where `n` is a power of 2: `nextMultiple = (orig + n - 1) & ~(n - 1);` – fge Dec 01 '14 at 17:52
  • Thanx fge a lot. I will go in details of that and come back to you – M Sach Dec 01 '14 at 17:56

2 Answers2

9

Change your last snippet to

System.out.println((a = b) & (a = c));

The assignment operator (=) has lower precedence than the boolean logical AND operator (&). Use parentheses to explicitly group your expressions.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Hu? Is it not a bitwise and for boolean. (Yes there are only 1 bit, but you can still bitwise and them). – MTilsted Dec 01 '14 at 17:24
5

This is because = has a lower priority than & (which, by the way, is a boolean operator in your snippets and not a bitwise operator; it is the same as && except that it does not short circuit).

Therefore your expression reads (with parens):

a = (b & a) = c

But you can't assign c to b & a.

fge
  • 119,121
  • 33
  • 254
  • 329
  • Thank you fge!!!! According to the [link](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html) you are absolutely right. And after more than one year of programming in Java, now I know operators precedence order))). And reading more [link](http://stackoverflow.com/questions/6800590/what-are-the-rules-for-evaluation-order-in-java/6801431#6801431) got that Order of evaluation of subexpressions is independent of both associativity and precedence. – serg Dec 02 '14 at 12:30
  • Well, anyway, the rule of thumb is always the same: if you don't remember about operator precedence, chances are that parens are "king", so use them when you are not sure ;) – fge Dec 02 '14 at 12:42