• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 6926
1316
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok get it now

Thanks to all who have helped
 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic