• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

understanding operator precedence and evaluation ?  RSS feed

 
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please refer the link http://radio.javaranch.com/corey/2004/07/12/1089659625000.html. it has two examples given below :





i'm not able to understand the difference between operator precedence and evaluation. to be specific what does evaluation means. i understand that operands are first evaluated from left to right and then operator precedence is applied. but i'm not understanding what is evaluation with respect to both the examples. please explain how are operands got evaluated in both the examples and what does evaluation means ?

also the oracle tutorial given at http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html says that

Thee operators in the following table are listed according to precedence order. The closer to the top of the table an operator appears, the higher its precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. Operators on the same line have equal precedence. When operators of equal precedence appear in the same expression, a rule must govern which is evaluated first. All binary operators except for the assignment operators are evaluated from left to right; assignment operators are evaluated right to left.



this is opposite to what is writter in Corey's SCJP timeline. please help to remove my confusion ??
 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

gurpeet singh wrote:



here space done the trick.

++a = 2(hence preincrement) and now a is 2 and a++ = 2(post increment) => 4

gurpeet singh wrote:



seriously, you need to delve in operator precedence chapter http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
 
Saloon Keeper
Posts: 10211
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

gurpeet singh wrote:i understand that operands are first evaluated from left to right and then operator precedence is applied.



No. Operator precedence is always determined first. This is necessary, because you won't even be able to evaluate an operation if you don't know what operands belong to what operators. As a matter of fact, operator precedence is determined at compile time, whereas evaluation is (obviously) done at runtime.

Evaluation simply means that the results of an expression is being computed. This always happens from left to right. I'll explain using your example.
Before the program is run at all, the compiler already determines how this expression needs to be evaluated. You can imagine this as if the compiler inserts parentheses to make it unambiguous what happens exactly:

++a and a++ get parentheses because they bind more tightly to 'a' and 'a' than the + operator does, because they have higher precedence. Then their respective results are passed to the + operator, because + has higher precedence than =.

So after the compiler is done, and the JVM runs the program, all that's left is evaluation. Precedence doesn't play a role anymore, because all the parentheses have already been inserted. Now, the fact that evaluation is done from left to right doesn't really matter for simple expressions. It only matters for conditional expressions or operations that have side effects. a++ is an example of an operation with a side effect, but don't forget about method calls!

Let's take a look at
You'd expect the last expression to evaluate to 5, with x becoming 5 and y becoming 3.
This however is not the case. This expression will evaluate to 4, with x becoming 4 and y becoming 3.

Why? Here's why:
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

gurpeet singh wrote:i understand that operands are first evaluated from left to right and then operator precedence is applied.



No. Operator precedence is always determined first. This is necessary, because you won't even be able to evaluate an operation if you don't know what operands belong to what operators. As a matter of fact, operator precedence is determined at compile time, whereas evaluation is (obviously) done at runtime.

Evaluation simply means that the results of an expression is being computed. This always happens from left to right. I'll explain using your example.
Before the program is run at all, the compiler already determines how this expression needs to be evaluated. You can imagine this as if the compiler inserts parentheses to make it unambiguous what happens exactly:

++a and a++ get parentheses because they bind more tightly to 'a' and 'a' than the + operator does, because they have higher precedence. Then their respective results are passed to the + operator, because + has higher precedence than =.

So after the compiler is done, and the JVM runs the program, all that's left is evaluation. Precedence doesn't play a role anymore, because all the parentheses have already been inserted. Now, the fact that evaluation is done from left to right doesn't really matter for simple expressions. It only matters for conditional expressions or operations that have side effects. a++ is an example of an operation with a side effect, but don't forget about method calls!

Let's take a look at
You'd expect the last expression to evaluate to 5, with x becoming 5 and y becoming 3.
This however is not the case. This expression will evaluate to 4, with x becoming 4 and y becoming 3.

Why? Here's why:



Thanks Stephen. but still i have doubts. in your example you said "++a and a++ get parentheses because they bind more tightly to 'a' and 'a' than the + operator does, because they have higher precedence. Then their respective results are passed to the + operator, because + has higher precedence than =. ". among these two operations i.e. ++a and a++ , which will be evaluated first. as per javadocs operator precedence chart, post increment operators have higher precedence than pre increment , which means a++ will get evaluated first. but if you refer corey's scjp timeline, he says exactly opposite. he says that first operand evaluation will occur which is always from left to right. further he says, ++a will be evaluated . i'm really confused who is right and who is wrong.
 
Stephan van Hulst
Saloon Keeper
Posts: 10211
216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

gurpeet singh wrote:as per javadocs operator precedence chart, post increment operators have higher precedence than pre increment , which means a++ will get evaluated first.



No. Precedence has nothing to do with order of evaluation. Precedence determines which operands bind to which operators. We have already determined that at compile time. Since in your example the pre- and post-increment operators don't compete over the same operand (they are both applied to a separate occurrence of 'a') the compiler doesn't even need to compare the two for priority. In your example, ++a gets evaluation before a++ because the first one is on the left, and Java evaluates from left to right. Precedence only comes into play if they compete over the same occurrence of an operand. For example: -a++;

In the above example, the expression will get evaluated (from left to right) as -(a++) because post-increment has higher precedence than negation. Operator precedence only determines *what* is evaluated, not *when* it is evaluated.
 
Greenhorn
Posts: 19
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:
Let's take a look at
You'd expect the last expression to evaluate to 5, with x becoming 5 and y becoming 3.
This however is not the case. This expression will evaluate to 4, with x becoming 4 and y becoming 3.

Why? Here's why:



Even though this thread is a bit old it's probably worth supplementing the knowledge here with the JLS. It spells this out quite nicely IMHO (emphasis below is mine).

From https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.7.1


The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.

If the operator is a compound-assignment operator (§15.26.2), then evaluation of the left-hand operand includes both remembering the variable that the left-hand operand denotes and fetching and saving that variable's value for use in the implied binary operation.



There are some really good examples in §15.7 as well.
 
Let me tell you a story about a man named Jed. He made this tiny ad:
ScroogeXHTML - small and flexible RTF to HTML converter library
https://coderanch.com/t/710903/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!