File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about System.out.println() in a non-void method

 
Santiago Bravo
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,


following code from Devaka Exam Simulator:




I thought the answer would be "1 Sub2" but the correct answer is "Sub1 2". The explanation said something about the System.out.println()
statement being evaluated beforehand but i didnt fully understand this.

Can anyone explain why the answer is "Sub1 2"?

I coded this up and replaced LINE 1 with:




which prints "1Sub2"


Thanks in advance
 
Henry Wong
author
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The print() methods takes *one* parameter -- a single string. And it prints the string.

It doesn't see the "1", or the " ", or the "2", independantly. All the components of the string concat are done before the print() method call. All that is passed to the print() method, is the result of the string concat.

So, any side effects of the string concat, including the call to the getI() method are done before this print() method call.

Henry
 
Fabio Nascimento
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.print(ga.i+" "+ga.getI());

First it execute the ga.getI() method overriden by the Arabik class which prints "Sub" and returns 2, then it prints 1 (ga.i) and 2 (returned by ga.getI()), so the final output is:

Sub1 2
 
Santiago Bravo
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so the print() method does a concat before the method call. So if I do something like this in the main:




This gives the correct answer that was given in the mock question: "Sub1 2"

Now this leads me to another question, why does the concat() method evaluate the string to be "Sub1 2" and not "1 Sub2"?



Thanks
 
ioanis vincent
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe this happens because there is a method call and paranthesis operator () has higher priority compared to concatenation operator +.
web pagethen c.
 
Santiago Bravo
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so you are saying whenever there is a method call in a print() method, it will be evaluated first regardless of any other calls?
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4116
195
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for discussing this question,

I�m sorry to here that my explanation (in the simulator software), was not helpful to you.

The key point you have to understand is, the argument you have pass to ANY method, will be evaluated, before sending it to the method. Consider the following example:


AnObject a;
anyMethod( a.getX() + a.getY() );


Before passing any argument to this anyMethod, the JVM have to evaluate it, and then send the result of the expression a.getX()+a.getY() to the anyMethod. In this case, if there were any System.out.println statements inside the getX() or getY() method, those statements will be executed while the above evaluation. After that, the result of the above expression will be sent to the method. So, before executing any statement inside the anyMethod, the statements inside the getX() and getY() method should be executed as first.
 
Santiago Bravo
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok get it now

Thanks to all who have helped
 
Brian Legg
Ranch Hand
Posts: 488
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get it now too... totally missed the fact that getI() had a print statement in it and was to focused on the print statement in main(). :roll:

Thanks Devaka
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic