File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Operator Precendence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Operator Precendence" Watch "Operator Precendence" New topic
Author

Operator Precendence

Enzo Del Mistro
Greenhorn

Joined: Jan 16, 2003
Posts: 2
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.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Enzo Del Mistro:
I calculate it with the following precedence:
(3) + ((1)) + 3 = 7

Why?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.


Java API Documentation
The Java Tutorial
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366

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
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I think Sri has it totally correct and explained nicely!
Enzo Del Mistro
Greenhorn

Joined: Jan 16, 2003
Posts: 2
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

Joined: Dec 06, 2001
Posts: 3061
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

Joined: Dec 06, 2001
Posts: 3061
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

Joined: Dec 06, 2001
Posts: 3061
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
Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
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 ().


May the force of the Java be in all of us !!!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Operator Precendence