Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# operators

priya rishi
Ranch Hand
Posts: 119
i have a very simple code:

class t1
{
public static void main(String args[])
{
int i = 3;
System.out.println(i*=2 + i++);
}
}

result is 15 ,

but if we use brackets ,
System.out.println((i*=2) +( i++));

then the result is 12.

Campbell Ritchie
Sheriff
Posts: 50687
83
Don't write code whose intention isn't obvious when you read it. You can get all sorts of results you don't expect!

Go and find a precedence chart (there is one here) and you see that the ++ postfix operator has the highest precedence of all. The + operator has a rather lower precedence, and the assignment operators including *= have the lowest precedence of all. So you can now work out which operation is carried out first, which is carried out second and which third. You should easily work out how you get 15. Remember 15 = 3 * 5.

When you add the round brackets () you are changing the precedence of the operators, so you can work out which operation is done first. That is more difficult to understand; I would have thought you would get 9. I shall let you explain how you get 15 and 12; remember what the difference between i++ and ++i is.

Vikas Kapoor
Ranch Hand
Posts: 1374
Campbell Ritchie wrote:...When you add the round brackets () you are changing the precedence of the operators, ...
He meant round brackets () have highest priority.

Rob Spoor
Sheriff
Posts: 20708
68
System.out.println(i *= 2 + i++) can be broken up as follows:

Whereas System.out.println((i *= 2) + i++) can be broken up as follows:

As said before, () binds the strongest. Furthermore, operands are evaluated from left to right, and i++ returns the old version of i not the new one.

Campbell Ritchie
Sheriff
Posts: 50687
83
Thank you, Rob. I worried and worried about that last night, wondering why it wasn't 9, then just after going to be I realised that the *=2 bit is executed first and the whole thing evaluates to 6+6, as you explained. Relieved that it follows the usual rules of arithmetic and the i++ operator, I was able to sleep!

priya rishi
Ranch Hand
Posts: 119
hello rob prime,

Whereas System.out.println((i *= 2) + i++) can be broken up as follows:

because the compiler assigns the value of i as follows:

i = 6 (after i*=2)
i = 6(after i++)
i=7(effect of i++)

so this code will have output as follows:

output is:

12
i is : 7

This is what i get , any suggestions are welcome.

Campbell Ritchie
Sheriff
Posts: 50687
83
I told you last night: remember the difference between i++ and ++i.

If you start with i as 6, then both i++ and ++i change i to 7. But you do not have i in that statement; you have i++. The value of i++ is the old value of i, so i is 7, but i++ is still 6.

Rob Spoor
Sheriff
Posts: 20708
68
You're right, i is 7, not 13. I accidentily mistook that "tmp = i + i" as an increase to i, but of course it isn't.

priya rishi
Ranch Hand
Posts: 119
Thanks , i am now good with this code.

Campbell Ritchie
Sheriff
Posts: 50687
83
Your'e welcome