• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Parenthesis in operator precedence  RSS feed

 
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
Marshal
Posts: 38659
677
Eclipse IDE Java VI Editor
  • 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: 60765
189
  • 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
Sheriff
Posts: 23601
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor 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: 60765
189
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!