Hi Shankar
Here is my take on 'pass by reference'
theory.
Here are the absolute statements about 'pass by
reference'
1) Pass by
reference works for all Classes, except Strings and arrays.
[Shankar] Pass by
reference works for all objects(incl arrays which are also
objects
be it even primitive arrays.as by definition anything on the
heap is an object and anything
"new"ed
is on the heap) And by inlining the code in the
methods that am calling am
able to change the value of the variables or
objects to be precise. So why not from
inside a method?
[Madhav]
Oops. My mistake. there is no
exception for Strings or Arrays.
If you had run my code, its clear
that inlining the code gives the same result as calling the
method. i.e, chaging the value of
variables is not possible in both the cases.
SO, the theory is
If you assign the new object (created
with new ) to the function argument (which refers to the
calling method variable) and modify its
content, it doesnt modify the calling method's
variable value, irrespective of whether calling method's variable is
passed by value or passed by
reference.
[Shankar] this is wot i infered from the behaviour of the code So
then coming back to the "pass by reference"
for objects. This behaviour says its a "pass by value" and "pass by
reference" correct?
[Madhav]
Its always pass by reference if
we accept all the objects(that are newed) are passed by
reference.
Another interesting fact is a swap method (like the one u write in C
to swap an integer value)
cannot be written in Java just for this one small
behavioural issue of the language. I dont know
if
its an issue for starters. u cant just swap two strings inside a
called method cos of this.
[Madhav]
Yeah. right. I see this because
Java doesnt have pointers. In C you can have local variables of a
function
refer to calling method's
variable value, where in java, you can only
have the local variables refer the calling method's variable (which is a
reference itself).
To change your membership options, refer to: