*
The moose likes Beginning Java and the fly likes Operator Presendence 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 » Java » Beginning Java
Bookmark "Operator Presendence" Watch "Operator Presendence" New topic
Author

Operator Presendence

ankit maini
Greenhorn

Joined: Feb 08, 2011
Posts: 25




According to the Operator Precedence < has higher precedence than || so it should solve first and output should be 4. but the answer is 3 WHY?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

|| is a short circuit operator- Which means is the left hand side is already true, it will not go to the right hand side to evaluate.

So is &&. You can find related posts in the forum.


Mohamed Sanaulla | My Blog
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Because of the operator precedence, line 2 is the same as this:

But the expression on the right side of the || will not be evaluated, exactly as Mohamed explained.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

As I understand it, precedence is not the same as order of evaluation.

Precedence dictates how "underparenthesized" expressions using different operators should be parenthesized. In other words, if an operand is between two different operators, then precedence dictates which operator "implies" parentheses.

In the expression...

...the precedence of < over || implies parentheses of...

...as opposed to...


But Java's order of evaluation dictates that the right operand of || will be evaluated only if the left side is false. (See JLS - 15.24 Conditional-Or Operator ||.)

Also see Precedence vs Associativity vs Order.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Operator Presendence