• 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

Operator precedence

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Previous discussion here.

I don't understand, why precedence x-- versus ++x is discussed regarding following expression:

Multiplication and division are left-associative and have lower precedence than increment/decrement. So, first we compute value of (++lion),  then evaluate constant 5, then compute multiplication ++lion * 5, then compute value of (lion--), and in the end perform division:

Even more,  precedence of x-- versus ++x have meaning only when we compare these operators with others (operator A is higher in the table, than operator B, operator B is higher than operator C, so operator A is higher than operator C), because Java doesn't allow using them simultaneously.
Do I miss something?
 
Marshal
Posts: 69495
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch and thank you for the old link.

Viacheslav Yakovenko wrote:. . . Do I miss something?

Yes, that this is cert exam revision code. which is allowed to be poor quality so as to help people prepare for the exam. The question is to test people's understanding that Java® code is executed left to right, and also that i-- has the same value as the old value of i.

Since that question appears here frequently, there are obviously many people who don't understand execution order nor the difference between ++i and i++. People think that lion is incemented, then decremented, then the multiplication by 5 occurs, and last the division. What you described is the correct order of execution. As I said before as long as there isn't an overflow error (or division by 0) tiger will become 5.
 
Viacheslav Yakovenko
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!
One minor clarification - not all Java code is executed left to right. For example, assignments are executed from right to left.
 
Campbell Ritchie
Marshal
Posts: 69495
277
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shall have to check in the Java® Language Specification (=JLS).

That JLS chapter wrote:15.7. Evaluation Order

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

So there is a bit of flexibility as an implementation detail, but you cannot distinguish the result from left to right execution. I think that applies to assignment, but remember that the assignment operator = associates to the right, so its execution won't complete until after its right operand has been completely evaluated. If you scroll down that JLS link, you will find that,

15.7.3. Evaluation Respects Parentheses and Precedence . . .

 
Viacheslav Yakovenko
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I really mixed up evaluation of operands and operator precedence, especially for assignments.
Hopefully, next code sample will help someone else to sort things up
 
Campbell Ritchie
Marshal
Posts: 69495
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Viacheslav Yakovenko wrote:. . . next code sample will help someone else to sort things up . . .

Well, it shows the exceptions thrown as you predicted, but I am sure it will cause somebody else no end of confusion
 
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this shows pretty much the same thing (copy-paste it into jshell - https://tryjshell.org):
 
Viacheslav Yakovenko
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to show how Java's evaluation of operands (always from left to right) corresponds to right associativity of assignments. Exceptions are just a method to stop execution at the right moment (debugging via exceptions )

is equivalent to

First we evaluate 'a', then evaluate 'b', then evaluate 'c', then execute assignment b = c, then execute assignment a = (b = c).
Plus, this code sample shows that Java first computes index in array, then computes new value, then performs range check, then assigns new value. It gives us a way to stop execution during assignments.

First versions of my code sample had similar to the peek() method. I can't find good explanation why I switched to exceptions Example for jshell nails my idea.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic