This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Output needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Output needed" Watch "Output needed" New topic
Author

Output needed

Ankith suresh
Ranch Hand

Joined: Jun 07, 2007
Posts: 42
int b=9;
b = b +(b=3);
System.out.println(b);
Output??
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Ankith suresh
Ranch Hand

Joined: Jun 07, 2007
Posts: 42
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

Joined: Aug 03, 2002
Posts: 7729
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Output needed
 
Similar Threads
short circuit operators
Please explain the output of code below
Output???
Abstract Class and Initialisation block
what is the flow of this program?