Awesome description of Java Pass by Value and Pass by Reference

You can find more here : http://www.cse.yorku.ca/~mack/1011/PassByReference.PDF
But I liked this part from the article, which is to the point :
Returning to the topic of this section, pass by value refers to passing a constant or a variable
holding a primitive data type to a method, and pass by reference refers to passing an object
variable to a method. In both cases a copy of the variable is passed to the method. It is a copy of
the “value” for a primitive data type variable; it is a copy of the “reference” for an object variable.
So, a method receiving an object variable as an argument receives a copy of the reference to the
original object. Here’s the clincher: If the method uses that reference to make changes to the
object, then the original object is changed. This is reasonable because both the original reference
and the copy of the reference “refer to” to same thing — the original object.
There is one exception: strings. Since String objects are immutable in Java, a method that is
passed a reference to a String object cannot change the original object. This distinction is also
brought out in this section

In

Leave a Reply

Your email address will not be published. Required fields are marked *