We all know that
Java uses Pass by Value and not Pass by Reference, as illustrated by the output of the following snippet :
Output :
Before - a=10
After - a=10
This is as expected as we are trying to modify a copy & the original object is not affected.
But while trying the following snippet :
I was expecting the output to be :
Before - a.size()=0
After - a.size()=0
But the output is :
Before - a.size()=0
After - a.size()=1
Why this difference in behavior?
I could only think of the following :
In the first case the Object instance is copied and this copy is acted upon by function '
test', but in the second case a copy of the reference variable is sent thus we have two references to the same object instance(ArrayList, in this case) - is this what is actually happening?