haraprasad mohapatra wrote:When we call the method at line 15,arr got a new object...
Java passes its arguments by value, not by reference. That means that the
calc method received a copy of the value in
arr. Now, this may seem confusing, because you have an
arr that is local to your
main method, and another
arr that is local to your
calc method. When
calc is called at Line 15, the value in
main's
arr is copied to the
arr that is
calc's argument. Changes to that argument made in
calc only change that argument; they don't change the contents of the variable from which the argument was copied. So, even though the copied value (which is
null) is replaced by a reference to an array of integers at Line 6, that only changes the value of the variable called "
arr" that is local to
calc. It doesn't change the value of the variable called "
arr" that is local to
main. They are two different variables, one initially receiving a copy of the contents of the other.