Hello lakshmi, I have tried your code like this way. its working fine and no problem of operator precedence. Let me explain you a little

x>3 returns false y++<2 returns false mone() returns true and if statement looks like this False && False | true

Now according to java presedence rule | operator has the higher precedence so False | true returns true and if statment looks like this after solving | operator False && true now result is false and value of x will not be print.

Please reply if there is still any confusion.

Thanks

Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74

posted

0

Hi Awais Sheikh ,

According to that the value of y after that condition must be 6.But why it is 5?

This is from the Java Language Specification 15.23

The && operator is like & (�15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true. It is syntactically left-associative (it groups left-to-right). It is fully associative with respect to both side effects and result value; that is, for any expressions a, b, and c, evaluation of the expression ((a)&&(b))&&(c) produces the same result, with the same side effects occurring in the same order, as evaluation of the expression (a)&&((b)&&(c)).

Each operand of && must be of type boolean, or a compile-time error occurs. The type of a conditional-and expression is always boolean.

At run time, the left-hand operand expression is evaluated first; if its value is false, the value of the conditional-and expression is false and the right-hand operand expression is not evaluated. If the value of the left-hand operand is true, then the right-hand expression is evaluated and its value becomes the value of the conditional-and expression. Thus, && computes the same result as & on boolean operands. It differs only in that the right-hand operand expression is evaluated conditionally rather than always.

Ever operator has a precedence (a number) associated with it. The precedence determines which operations will be performed first. "|" has higher precedence than "&&", Therefore: (a && b | c) is the same as (a && (b | c)), not ((a&&b)|c). In your case, a is false, then stop to evaluate (b|c), so that is why y is not 6.

Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74

posted

0

Hi,

I thought in the expression (++b+(b*c)),(b*c) is evaluated before ++b.But now i understood the operands are evaluated from left to right. But why parantheses has no higer precedence here.