Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Multiple assignments on the same line

 
Ranch Foreman
Posts: 45
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

It seems that I am struggling more than I expected in my preparation of the OCA exam...

While doing a mock exam, the following block of code tricked me.



I was expecting this to throw an ArrayIndexOutOfBoundsException because i is assigned the value of 30 and hence, iA[30] does not exist. However, it prints 30 20 30.

Could someone explain me why this thinking is flawed?

Many thanks!!!
 
Saloon Keeper
Posts: 10308
217
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you're confusing operator associativity with order of evaluation.

The = operator is right-associative, so the expression is equivalent toPeople have the tendency to evaluate expressions by performing operations in parentheses first. That's not how Java works. Java always evaluates left-to-right, meaning the i in iA[i] is evaluated to 0 before i = 30 is evaluated, and finally 30 is assigned to iA[0].
 
Brecht Geeraerts
Ranch Foreman
Posts: 45
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hum, this is more complicated than I anticipated. I'm not sure I fully grasp the difference...

On a university website I read the following:

Regarding associativity: "When an expression has two operators with the same precedence, the expression is evaluated according to its associativity. For example x = y = z = 17 is treated as x = (y = (z = 17)), leaving all three variables with the value 17, since the = operator has right-to-left associativity (and an assignment statement evaluates to the value on the right hand side)."

Order of evaluation of subexpressions: "Associativity and precedence determine in which order Java applies operators to subexpressions but they do not determine in which order the subexpressions are evaluated. In Java, subexpressions are evaluated from left to right..."

which confused me even more...



 
Stephan van Hulst
Saloon Keeper
Posts: 10308
217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Precedence and associativity tell the compiler which operands go with which operators. You can kind of compare it to the compiler inserting explicit parentheses in the code, as the example x = (y = (z = 17)) demonstrates. This happens during compilation.

At runtime, when the expression is evaluated, Java has no idea about precedence and associativity. It just sees a tree of expressions that consist of sub-expressions. Those sub-expressions are evaluated left-to-right.

The easiest way to do this yourself is to first write down the complete expression and inserting parentheses everywhere until you can tell which operand goes with which operator without having to think about precedence rules. Then repeatedly scan the expression from left to right until you find a complete sub-expression. Evaluate it, and then rescan the resulting expression.

In the example given by the website, the first complete sub-expression is (z = 17). In the example you gave at the start, (iA[i]) is the first complete sub-expression.
 
Sheriff
Posts: 13513
223
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't the first sub-expression be whatever was inside the brackets on the left side of the assignment? If you change it slightly, you'd have (almost) the same expression tree.
 
Brecht Geeraerts
Ranch Foreman
Posts: 45
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the additional clarifications. It is much appreciated!!
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!