I know that java is return by value not by reference, so it makes a copy of the object and passes the new copy. Is this is the case with the return statements also?
Does the
return obj;
creates a new object copy or simply returns the current object itself.
No, this is a wrong assumption. It’s true that Java passes everything by value, but this means the following:
Scalar variables contain values, so you can easily see that they are passed by value.
Object variables, on the other hand, do not contain objects. You could think of them as containing a pseudo-address to an object. That pseudo-address is copied (by value), but it still points to the same object. (Objects are actually created only using a
newexpression.)The above is true (for both types of variables) for assignments, arguments and return values.