1

Through Java operator precedence table:

'|' Logical OR operator has higher precedence than '&&' logical AND operator.

I checked above fact using following code

int y = 5; int x = 2; if( x++ > 2 && ++y > 2 | true ) ; //do nothing System.out.println("x = " + x + " y = " + y); 

but above line giving output as -

x = 3 y = 5 

showing that x++ is evaluating first.

Even I put parentheses at condition around |

if( x++ > 2 && (++y > 2 | true) ) ; 

But still I am getting the same output.

Why operator precedence not working in this case?

0

6 Answers 6

5

That's not the logical operator. That's the bitwise operator. It will evaluate everything - that is, it won't short circuit - and if anything flips that bit to 1, it'll stay at 1 until negated.

Here's how these statements would evaluate:

  • x++ > 2 && ++y > 2 || true -> true. We fail with the logical AND, but succeed with the logical OR. With short circuiting, we don't continue to evaluate any portion of the logical AND, since x > 2 is false.

  • x++ > 2 && (++y > 2 || true) -> false, since we will short-circut due to x > 2 not being true.

If you actually don't want the short circuit behavior, then use the bitwise AND as well, and you'll get your expected evaluation block.

x++ > 2 & ++y > 2 | true will still evaluate to true, but the values of x and y will change to 3 and 6, respectively.

Sign up to request clarification or add additional context in comments.

4 Comments

Actully I used | oprator intentionally, and if | operator has high precedence than &&, then why && is executing prior than |.
I don't think it does. You're applying all of the bitwise operations on either side of the logical AND, then applying the logical AND. You're also getting a bit of a red herring - you don't actually do anything in the logic block, so how can you be absolutely certain you're not getting the result you want?
because if | evalute prior than &&, then y should have 6 as output.
No. You're short-circuiting on &&. The right hand side isn't evaluated.
2

| is the bitwise OR operator. You're looking for ||. It's similar, but differs in that it has higher precedence and does not apply short circuit evaluation.

I see what you're really asking now. You're wondering why if && has the least precedence, the rest of the statement isn't evaluated before finally coming to it. So in

x++ > 2 && ++y > 2 | true 

it should evaluate x++ > 2, then ++y > 2 | true and finally apply &&. Well, the answer is that && applies short circuit evaluation. Sure, it can evaluation everything and then apply its effect, and that's what the bitwise operator does. However it doesn't because

if (a && b && ...) 

is supposed to behave similarly to

if (a) { if (b) { ... } } 

Operator precedence is as you expect, however, the evaluation is terminated early because of the property of the operator. So going back to

x++ > 2 && ++y > 2 | true 

We see that x++ > 2 is false, so ++y > 2 | true is not evaluated.

3 Comments

No I looked for | operator. And if | operator has high precedence than &&, than why && is executing prior than |.
@VijendraSingh I understand what you mean now. I've edited my answer.
@ZongLi you can show how the code is parsed with parantheses. (x++ > 2) && (++y > 2 | true).
1

try this

if( x++ > 2 && ++y > 2 || true ) 

You are using bitwise operator not logical operator

Operators in java

Comments

1

Even if the operator | has higher precedence, the operator isn't even discovered at the point where the program checks if calculating the right-hand side (of &&) is necessary.

Consider the statement (true && true | true). This is how it is calculated:

  1. Check (true && ...) to see if further operations are necessary, (which is the case).
  2. Higher precedence: Perform the operation (true | true) -> true.
  3. Lower precedence: Perform the operation (true && true) -> true.

In your case, since (x++ > 2) gives false, the right-hand side of && is never even touched.

Comments

0

Since whatever you place the braces , it will start from left to right because it in case of && , it is optimized to check first condition , it get false , then why to go to second condition , no matter braces are there ,secondly x++ will get executed , condition get false , so it is printing the same output but when you do let say using ||

class Test{ public static void main(String[] args) { int x=2,y=5; if( x++ > 2 || (++y > 2 | true) ) ; System.out.println(x +" "+y ); } } 

It will print 3 6 because || takes place , first condition get false but when it comes to second , ++y > 2 evaluated to true | between boolean values give output on basis of bitwise OR , true OR true is true .

Comments

0

The logical operators: (&& , ||, &, |, and ^) can be used only to evaluate two boolean expressions.

The difference between && and & is that the && operator won't bother testing the right operand if the left evaluates to false, because the result of the && expression can never be true.

The difference between || and | is that the || operator won't bother testing the right operand if the left evaluates to true, because the result is already known to be true at that point.

Bitwise operators: (&, |, and ^) can also be called as "Bitwise" operators. Bitwise operators compare two variables bit by bit, and return a variable whose bits have been set based on whether the two variables being compared had respective bits that were either both "on" (&), one or the other "on" (|), or exactly one "on" (^).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.