• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A simple looking System.out.println problem

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Regards,
HArneet
---------------------------
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() {
System.out.println("Parent");
return wealth;
}
}
-------------------------------------
 
amol re
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
HTH
 
WiLL Tao
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good question and Good answers!
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and bad name
harneet,
Welcome to Javaranch
Please read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you.
 
Jim Bedenbaugh
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic