This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

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
Posts: 39129
723
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: 62863
203
  • 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: 23813
140
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: 62863
203
  • 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.
 
Trust God, but always tether your camel... to this tiny ad.
java tutor first lesson for free
https://coderanch.com/t/702613/java-tutor-lesson-free
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!