Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Operator question

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got this from Enthuware's SCJP 6 suite:



The correct output is 6 when you run the above with no arguments. I thought it would be 7, because:
Since the preincrement operator has higher precedence than the assignment operator, that is executed first, and then you have:
k += 3 + 2
Then, the assignment is executed, but at this point k has value 2, after the preincrement operator has executed, so that would give:
k = k + 5 => k = 2 + 5 => k = 7

Instead, it seems the compound assignment operator gets expanded before applying the preincrement. I don't understand this because of the precedence issue. Could anyone explain?

Thanks.
[ December 24, 2008: Message edited by: Ruben Soto ]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! I also have an inquiry regarding this topic. I have the following code:




I was thinking that it had to increment the x first before equating it to the y because of the higher precedence rules, yet it did not, it equate first the x to y then increment the x...

Output:
TOINKZ1

Can anyone explain?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Ruben Soto.
The correct output is 6 when you run the above with no arguments. I thought it would be 7, because:
..........
k+=3+ ++k ===>> k=k+(3+ ++k)===>> k=1+(3+ 2)===>> k=6;
on the right hand side, initially ++k becomes 2 but it dont assign this new value to k. Because in any expression the value of a variable is modified only once and also the (last) modified value will be assigned to the variable at the end of the execution of the expression.
more explanation is needed in this.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your input, Suresh. Did you get that from the JLS?
Here's what I got, which explains how compound assignment expressions are evaluated (JLS, 15.26.2)


� First, the left-hand operand is evaluated to produce a variable. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the right-hand operand is not evaluated and no assignment occurs.

� Otherwise, the value of the left-hand operand is saved and then the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.

� Otherwise,the saved value of the left-hand variable and the value of the right-hand operand are used to perform the binary operation indicated by the compound assignment operator. If this operation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.

� Otherwise, the result of the binary operation is converted to the type of the left-hand variable, subjected to value set conversion (�5.1.13) to the appropriate standard value set (not an extended-exponent value set), and the result of the conversion is stored into the variable.



All this is quite fascinating, and I wish I had time to read through the JLS a few times before my test, but unfortunately I don't have enough time.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jay Reyes:

I was thinking that it had to increment the x first before equating it to the y because of the higher precedence rules, yet it did not, it equate first the x to y then increment the x...


Hi Jay,

Here's the way prefix and postfix (both increment and decrement) work:
-Prefix operators: Change the value of the variable, and the changed value of the variable is used in the expression.
-Postfix operators: Change the value of the variable, but the value of the variable before the change is what is used in the expression.

This doesn't have anything to do with precedence. Indeed, the postfix operator has higher precedence than the assignment, but what the postfix operation evaluates to is the value of the operand before the change.
So:

See what's going on now?
[ December 24, 2008: Message edited by: Ruben Soto ]
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do we need to remember operator precedence as kathy sierra book for 1.6 says that it is not on the exam.
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do we need to remember operator precedence as kathy sierra book for 1.6 says that it is not on the exam.
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic