This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Output needed

 
Ankith suresh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int b=9;
b = b +(b=3);
System.out.println(b);
Output??
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you get when you compile and run it?
What don't you understand about the answer you get? Please show some effort that you have tacked the problem.
 
Ankith suresh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:
What do you get when you compile and run it?
What don't you understand about the answer you get? Please show some effort that you have tacked the problem.


I excuted and got the output is 12.
But i want to know the flow of excution happening here

b=b+(b=3)

According to operator precedence (b=3) has to excute first rt?

so b= 3 + 3 right?

So answer is 6 ???
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, take a look at how b = b + (b=3) is performed.
First the value of b(9) is pushed on the stack.
Then the value of evaluating b=3 (3) is pushed on the stack.

So the stack (from the top down) contains 3 and 9.
The result is the sum of those two stack entries (12), and that gets assigned to b.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic