This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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.
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.
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.