I have two code snippets
code snippet 1
boolean flag = false;
System.out.println((flag = true) | (flag = false) || (flag = true));
System.out.println(flag);
code snippet 2
boolean status = true;
System.out.println(status = false || status = true | status = false);
System.out.println(status);
I am trying to understand the output of the above code snippet. I have read Operator Precedence in Java to know which one executes first and which one execute later.
The rule is bitwise OR > logical OR > assignment
However, I don’t understand why the first code snippet is valid and the second raise a error – super strange to me
E:udemyJava SE 11 DeveloperPractice-TestsTest1srcTest.java:215:43
java: unexpected type
required: variable
found: value
Why the first flag variable in
System.out.println((flag = true) | (flag = false) || (flag = true));
is gray on IntelliJ
I would like to understand how the logical operators and assignment work in this context.
1