Krishna Kishore wrote: ↑Wed Jul 24, 2019 4:04 am
Hello Paul
I have one doubt, You have mentioned that when there is a method inside the System.out.println(), then the method is first executed and then the other expression in the print statement. Is this always the case?
System.out.println(b.h + " " + b.getH());
So because of it
1. First b.getH() is executed
1.1. The print statement is executed first from the method
1.2 The int value is returned back
2. b.h is executed
3. Value from step-2 and step1.2 are concat to form 4 44
Is this correct ?
You are misinterpreting what I said. Think about it. println(...) is just a method call with some argument. This argument could be a value, or an expression or a method call. To invoke println, the argument (whatever it is) has to be evaluated first, otherwise, what will you pass to the println method? So, yes, if the argument is a method call then that method has to be invoked first.
In this case, the argument is not a method call but an expression b.h + " " + b.getH(). The standard rules of evaluating an expression will apply.
To compute this expression, b.h+ " " has be computed and then the result has to be added with b.getH(). So, after computing b.h+" ", you need to get the value of b.getH().