aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Operator precedence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Operator precedence" Watch "Operator precedence" New topic
Author

Operator precedence

Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74
Hi,
Here | has higer precedence than &&.But here why it is executing as((x>3)&& ((y++<2)|mone()))

Code is:
int x=2;
int y=5;
if((x>3)&&(y++<2)|mone())
System.out.println(x);
System.out.println(y);
Awais Sheikh
Ranch Hand

Joined: Jun 07, 2006
Posts: 48
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
Hi Awais Sheikh ,

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

Joined: Feb 07, 2005
Posts: 2367
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)).

ConditionalAndExpression:
InclusiveOrExpression
ConditionalAndExpression && InclusiveOrExpression

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.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
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
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.

Thanks.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
http://www.eskimo.com/~scs/readings/precvsooe.960725.html
Operator Precedence vs. Operand Evaluation
Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Operator precedence