• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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.
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic