Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Parenthesis in operator precedence

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I thought that parenthesis is the top priority to evaluate but this example prove wrong. Can anyone help me with how the parenthesis work in a complex evaluation, especially with && and ||?
 
author & internet detective
Posts: 39986
806
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nhien,
Parens are the highest order of precedence. You have some redundant ones which makes it harder to read. This is the same as your example:



As is:


The | (logical or) means Java has to evaluate the second expression. It doesn't use short circuiting so both sides are executed.
 
Marshal
Posts: 69373
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nhien Nguyen wrote:. . .

Did you write that code yourself or did you find it elsewhere? If elsewhere, please be sure to tell us where. Is the empty statement at the end of that line intentional?
Please explain why you thought that example proves you wrong. Please tell us what you thought would happen and what actually happened. I would expect to see 1 0.
 
Nhien Nguyen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Nhien Nguyen wrote:. . .

Did you write that code yourself or did you find it elsewhere? If elsewhere, please be sure to tell us where. Is the empty statement at the end of that line intentional?
Please explain why you thought that example proves you wrong. Please tell us what you thought would happen and what actually happened. I would expect to see 1 0.



Sorry i wasn't make it clear. That's question 9 from Selft-Test at Chapter 4 of OCA SE8 by Kathy Sierra and Bert Bates. The full snippet is:

I thought that that all operation in the innermost parenthesis will be evaluate first. But on line 6, the (++count<10), which is in the innermost parenthesis is skipped because of ||.
Also, line 8, should the > operator is evaluate before the &&?
 
author
Posts: 23877
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nhien Nguyen wrote:
I thought that that all operation in the innermost parenthesis will be evaluate first. But on line 6, the (++count<10), which is in the innermost parenthesis is skipped because of ||.
Also, line 8, should the > operator is evaluate before the &&?



Operator Precedence and Order of Evaluation are two different things. Do not confuse them.  In Java, Order of Evaluation is left to right, and can be short circuited by the logical operators.

Henry
 
Campbell Ritchie
Marshal
Posts: 69373
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nhien Nguyen wrote:. . . thought that that all operation in the innermost parenthesis will be evaluate first. But on line 6, the (++count<10), which is in the innermost parenthesis is skipped because of ||.

The expression inside the innermost () is evaluated; the definition of the || operator (=“conditional or”) is such that after its left operand evaluates to true, the code to its right is skipped. I think you would do well to revise the behaviour of ||.

Also, line 8, should the > operator is evaluate before the &&?

Why? The principal order of evaluation is left‑to‑right. The && operator is evaluated, but it has to wait for its right operand to be assessed before it completes. Since the left operand to && is true, it is necessary to execute the code to its right, too. That will make count 1. The code you posted originally is different from the question in the book.
 
All that thinking. Doesn't it hurt? What do you think about this tiny ad?
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic