• 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

Output needed

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int b=9;
b = b +(b=3);
System.out.println(b);
Output??
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic