• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Operator Precendence

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question regarding operator precendence. I have been trying questions in a JAVA programmer certification book that to me seems incorrect for an example. A colleague of mine calculates their answer in the same way that the answer specified in the back of the book. I however don't see it that way. The question is as follows:
int k = 1;
int i = ++k + k++ + + k;
System.out.println(i);
My colleague and the book answer calculates this with this precendence:
((2) + (2) + (3)) = 7
I calculate it with the following precedence:
(3) + ((1)) + 3 = 7
Both cases give the same answer but the precendence determination is different.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Enzo Del Mistro:
I calculate it with the following precedence:
(3) + ((1)) + 3 = 7


Why?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not familiar with the exact specification for this situation in Java. I know in C++ that it is undefined, though, because the statement is trying to change k in two different spots. The question is: what is the value for k before the second increment? C++ says it could be anything. From the answer the book has, it seems that k is incremented the first time to get 2. Since this is a pre-increment operator, the value 2 is used to calculate the value of i. When it is incremented the second time the final value of k is 3, but since this is a post-increment operator, the value 2 is used in calculating the value for i. So you would get the answer as the book states.
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


int k = 1;
int i = ++k + k++ + + k;
System.out.println(i);


I am assuming something here that
int i = ++k + k++ + ++k;
If my assumption is right , then i can explain why it is 2 + 2 + 3
as we have declared k =1 , in operator precedence prefix and postfix operators take precedence over arithmetic operators i,e (++ precedes +). That effectively means that ++k or k++ have to be calculated first before adding them up.
now
int k = 1 ;
int i = ++k + k++ + ++k;
in java , the calculation starts from left side.
the above expression is same as
int i = (++k) + (k++) + (++k);
when (1) ++k is called , immediately k value is incremented by 1 . so k = k + 1 = 2;
(2) now k++ = 2 (since the value of k increases after the statement for postfix operator.
(3) now calculate ++k, as we have not encountered the end of statement , after step (2) k remains 2.
in step (3) ++k hence becomes 3.
so int i = (++k) + (k++) + (++k) = 2 + 2 + 3
hence int i = 7;
Hope this explanation helps
Sri
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Sri has it totally correct and explained nicely!
 
Enzo Del Mistro
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the information. The expression is how I stated though:
int z = ++k + k++ + + k;
I also understand the left to right rule you mentioned. The question I have is that the documentation of precendence I have states that postfix has the same precedence as () which is higher than prefix, therefore I would suspect that k++ should be executed first? Therefore rendering 3 + 1 + 3. Is that valid reasoning?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sri Sri:
I am assuming something here that
int i = ++k + k++ + ++k;


I think this assumption is incorrect. It would produce a different result. See my next comment.

(3) now calculate ++k, as we have not encountered the end of statement , after step (2) k remains 2.


No, the postincrement operator still changes the value of k to 3. The difference is that the value 2 is used in the next operation (in this case when the + operator gets its turn).
[ January 17, 2003: Message edited by: Layne Lund ]
[ January 17, 2003: Message edited by: Layne Lund ]
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Enzo Del Mistro:
Thanks for the information. The expression is how I stated though:
int z = ++k + k++ + + k;
I also understand the left to right rule you mentioned. The question I have is that the documentation of precendence I have states that postfix has the same precedence as () which is higher than prefix, therefore I would suspect that k++ should be executed first? Therefore rendering 3 + 1 + 3. Is that valid reasoning?


As I understand it, the precedence rules only apply when two operators are fighting for the same operand. Since, in this case, the postfix and prefix operators are on different operands, they are fighting with another operator (a + in this case).
However, you have still raised a good question. Which operator increments first? As stated earlier, I know for sure that this kind of statement has undefined results in C++. Now I'm curious about the Java specification. I am willing to bet it is similar.
Layne
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, you piqued my curiosity enough to write a test program that can see what happens when this statement is executed:

This produced the following output:

Whether or not this behavior is consistent between implementations isn't necessarily guaranteed, though. You should check the actual Java specification to find out.
Layne
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think memorizing the operator precedence table is a waste of time. They wont ask you questions about which operator has higher precedence. What they will do is ask you at least one question on evaluating an expression. But you knot need to go into too many details. All you have to know is how the different operators work, lie a++, ++a, -a, +a, *,-,%, etc. And know that like operators have the same precedence. Its a helpful hint. Treat ++a as having the same precedence as a++. The rest of the operators *,/ have a higher precedence than +,-.
int a=1;
Evaluate int i=++a + a++ + +a;
So
i=preincrement a + postincrement a + positive a;
In the first part, the value of a is 2 since a is 1 and the preincrement says increment a before the rest of the expression is evaluated. So here a is 2
In the next part, the value of a is whatever it was last because the postincrement happens after this expression ++a + a++ is evaluated. This is what is meant by the postincrement operator has the same precedence as the (), because the value of a is incremented after this expression ++a + a is evaluated. So the value of a is 2 when this expression ++a + a is evaluated and then is bumped up to 3.
In the last part +a, the value of a is now 3, so the whole expression ++a + a++ + +a becomes (2)+(2)+(3) which gives 7.
I hope you understood what happens in the a++ part, and why they say postincrement (like postdecrement) has the same precedence as the ().
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Layne Lund:
I know for sure that this kind of statement has undefined results in C++. Now I'm curious about the Java specification. I am willing to bet it is similar.


And I am willing to bet that it's perfectly well specified in Java.
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic