In the code below, if the string \"Mult\"
comes before the test1(4)
method call, why does the method output before the string? And why does it bounce f
When
System.out.println("Mult:" + test1(4));
is run, test1(4) must be evaluated before it can be System.out.println'd. So, the JVM will run the following:
evalate result of test1(4) {
print ("N:" + 4);
return 2*4;
}
evalate result of "Mult:" + returnedValue {
print("Multi:" + returnedValue);
}
The JVM cannot print something before it knows its value. In this case, test1(4) get evaluated first so the JVM knows what to print out (the return value).
You could also complete the task this way>>
int result = test1(4); //This prints out 4 and evaluates to 8
System.out.println("Mult: " + result); //This concats "Mult: " and 8 after converting it to of type String
"Mult:" + test1(4)
The above line is an expression whose value is the concatenation of "Mult:"
, and of the result of test1(4)
. So, to be able to know the result of this concatenation, test1(4)
is executed. This execution prints "N:4"
, and then multiplies n by 2, returning 8. 8 is then concatenated with "Mult:"
, and the result is printed.
In the code below, if the string "Mult" comes before the test1(4) method call, why does the method output before the string?
Because you're calling the method before you're calling System.out.println
with the "Mult:" part.
Basically, your code is equivalent to:
public static void main(String[] args){
int tmp = test1(4); // This prints N:4
System.out.println("Mult:" + tmp);
}
If you think about it, the test1
method has to be completed before the string concatenation can occur (otherwise we can't know what the right hand side of the concatenation operator is), and the string concatenation has to occur before the System.out.println
call can occur (otherwise we can't know what we're going to print). Make sense/
The first thing to note is when you use the +
with two operands where one of the two operands is a String
, the result of the expression is a String
.
Therefore, in the following method invocation expression
System.out.println("Mult:" + test1(4));
you are invoking the PrintStream#println(String) since out
is a variable of type PrintStream
. Note how the method accepts a single String
argument. Therefore, the String
has to be resolved from the String
concatenation of
"Mult:" + test1(4)
For that to happen, the test1(4)
method has to be executed.
public static int test1(int n){
System.out.println("N:" + n);
return n*2;
}
This method again uses PrintStream#println(String)
but with the argument
"N:" + n
This is another String
concatenation that produces the String
value
"N:4"
for this particular invocation. The produced String
value is then used as an argument to the println(..)
method which outputs it to your program's standard output.
The method then returns the value 8
, since 4 * 2 = 8
.
That return value is the value of invoking the test1(4)
method. So
System.out.println("Mult:" + test1(4));
is equivalent to
System.out.println("Mult:" + 8);
Then String
concatenation occurs, transforming
"Mult:" + 8
into the String
value
"Mult:8"
That String
is then used as the single argument to the println(..)
method which outputs it to your program's standard output.
It obvious.If a method does not have its arguments calculated it cant be completed. In your case the println method must have its arguments computed before it can be executed. So the function call to test1 method is made which completes first. Then the statement in main is executed.