vendredi 6 avril 2012

real life example for passing argument by reference in java

I read this in one of the comments for this answer in SO http://stackoverflow.com/a/40523/612920

My attempt at a good way to visualize object passing: Imagine a balloon.
Calling a function is like tieing a second string to the balloon and handing the line to the function.
parameter = new Balloon(); will cut that string and create a new balloon (but this has no effect on the original balloon).
parameter.pop(); will still pop it though because it follows the string to the same, original balloon. Java is pass by value, but the value passed is not deep, it is at the highest level, i.e. a primitive or a pointer. Don't confuse that with a deep pass-by-value where the object is entirely cloned and passed.

Aucun commentaire:

Enregistrer un commentaire