• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

operators

 
priya rishi
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

i dont understand how i get 15.please help.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49845
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't write code whose intention isn't obvious when you read it. You can get all sorts of results you don't expect!

And please use the code button; it makes your posting easier to read.

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49845
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49845
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks , i am now good with this code.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49845
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your'e welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic