• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unexpected Output Sequence

 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the output of this program,
the first elements that are printed are the

System.out.println("Beta " + h) ;
in the method getH() of subclass Beta.

(The lines referenced are highlighted in the source code at the bottom)
On line 1, a reference variable of the parent class is declared.
On line 3, the reference variable of the parent class is cast to the type of the subclass 'Beta'.

Question:
Why does the System.out.println("Beta " + h); in the method getH() appear first in the output
when this method call
appears as the second element in the output statement in the 'main' method of line 2 ?




It is my observation that
'b.h' (A reference variable accessing a member field ) is the first thing that appears in the output statement

in the main method and should appear first when line 2 is executed in 'main'.






Any insight would be appreciated?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:
Why does the System.out.println("Beta " + h); in the method getH() appear first in the output
when this method call
appears as the second element in the output statement in the 'main' method of line 2 ?



Good question. The reason is that Java needs to evaluate all the arguments to a function before executing it. That means the flow is:
1) evaluate b.h
2) evaluation b.getH()
3) Concatenate the results of the previous two steps and the string with a blank
4) Call System.out.println() with the results of the previous step
 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your response.

Here is a slight alteration using the same concept as in the previous example.



Please verify that the reason the output appears first from the method


has to do with how

System.out.println();
generates output.


Please let me know if I have misunderstood your explanation.


Thanks.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right. That's not specific to System.out.println(). Any function runs after the arguments are evaluated.
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
println is just method so calling it conforms to the rules of calling a method defined in 15.12.4 of the JLS (http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.4)

At run time, method invocation requires five steps.
First, a target reference may be computed.
Second, the argument expressions are evaluated.
Third, the accessibility of the method to be invoked is checked.
Fourth, the actual code for the method to be executed is located.
Fifth, a new activation frame is created, synchronization is performed if necessary, and control is transferred to the method code.


So all the argument values are determined first before calling the method. Otherwise there is no knowing what values to call the method with.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic