aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about System.out.println() in a non-void method 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 "Question about System.out.println() in a non-void method" Watch "Question about System.out.println() in a non-void method" New topic

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

Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
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

My Path to SCJP Certification My Path to SCWCD Certification
Henry Wong

Joined: Sep 28, 2004
Posts: 18825

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.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Fabio Nascimento

Joined: Nov 16, 2008
Posts: 16
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

Joined: Jul 25, 2008
Posts: 226
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"?

ioanis vincent

Joined: Nov 17, 2008
Posts: 5
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

Joined: Jul 25, 2008
Posts: 226
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
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3090

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.

Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
ok get it now

Thanks to all who have helped
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
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

~Currently preparing for SCJP6
I agree. Here's the link:
subject: Question about System.out.println() in a non-void method