File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A simple looking System.out.println problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A simple looking System.out.println problem" Watch "A simple looking System.out.println problem" New topic

A simple looking System.out.println problem

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi All,
I tried to execute the following code.The output code was not as expected.Following is the peice of code along with expected and actual output.Can anybody expalin me the reason of actual output.
expected output : Harry Parent 100000
actual output : Parent harry 100000
public class Shadow {
public static void main(String s[]){
STParent stp = new STParent();
System.out.println("Harry " + stp.getWealth());
class STParent {
double wealth = 100000.00;
public double getWealth() {
return wealth;
amol re
Ranch Hand

Joined: Mar 21, 2002
Posts: 36
When the execution of the program reaches the line "System.out.println("Harry " + stp.getWealth());", i think it should be taking the following path:
1. The expression stp.getWealth() is evaluated to a string. While doing so, it prints "parent".
2. "Harry" is concatenated with the string to which stp.getWealth() has evaluated to. The result of concatenation is "harry 100000" which is a single sting which is printed on the console by System.out.println().
3. So, you see "parent harry 100000".
If argument to System.out.prinltn() is concatenation of string(s) and expression(s) ( expressions which evaluate to string ), will always evaluate all the expressions, concatenate all the resulting strings and print the entire result as one single string.
( System.out.prinltn( String string ) takes one single string as an argument. Its a different story that you are creating that one single string on the fly while passing it to the function. The string would still be "created" first before being passed. )
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5382
when you call a method inside a method then methods are put on a Stack

Stack is LIFO(Last In First Out). So let us see how the stack is when the code is executed.
*) first method invoked is S.o.p.01
*) second method invoked is S.o.p.02
So stack is like this

1) So the Last In method is S.o.p.02, so that will be executed first and prints "Parent"
2) Now S.o.p.02 is out of the stack. So stack is

3) NOw S.o.p.01 executed and at that time S.o.p.01 is like:
System.out.println("Harry " + 100000.00);
so it prints "Harry 100000.00"

"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
WiLL Tao
Ranch Hand

Joined: Dec 14, 2001
Posts: 56
Good question and Good answers!

Thank Folks who <b>Make Sense</b> here.<br />SCJP Platform 2
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
...and bad name
Welcome to Javaranch
Please read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you.

[Blog] [Blogroll] [My Reviews] My Linked In
Jim Bedenbaugh
Ranch Hand

Joined: Nov 09, 2001
Posts: 171
Originally posted by Ravish Kumar:
when you call a method inside a method then methods are put on a Stack

According to my understanding of the JLS, this happens a little differently than you explain, although we have come to the same conclusion : There are 3 operators in the expression + () (method call) and . By order of precedence, the . operator is evaluated first, followed by the ()(method call). The method executes and prints Parent, then returns the numeric value. Then, the invoking declarative is invoked printing the rest i.e. Harry 100000.00 on a separate line.
I suppose the JVM does put the methods on the stack as you stated. For me, evaulating by operator precedence does the trick. No big deal though.
[ March 26, 2002: Message edited by: Jim Bedenbaugh ]

I agree. Here's the link:
subject: A simple looking System.out.println problem
It's not a secret anymore!