Many people get confused with the way arguments are passed in Java. Parameters in Java are actually passed-by-value not pass-by-reference. But sometimes we get a feeling its pass-by-reference in case of objects being passed as parameter. Parameter passing is easy to understand for primitive values, but it gets confusing with references. Lets start with passing primitive values as parameter,
When we execute this code, the output will be
"i=5, j=6". So why didn’t the value of
j change when we passed them as an argument to
myMethod? This is because the value of
j was copied to
b. So changing the value of a and b doesn’t effect the value of
j. So even if
j were final, the code would work just fine,
This will again produce the output
j‘s values will be copied to
b, so the code would work just fine without any error that you are trying to change value of a final variable. When we change the value of
j are not effected. At a first glance someone might think that we are changing the value of final variables
j but we are not as can be seen from the output.
This whole thing gets a little tricky for object references (you can understand difference between object and reference here). Lets say we have a class like this,
Now we’ll pass an instance of the above class and see how passing an object reference as argument works,
So will you get a
NullPointerException on line 10? The answer is no. When you pass
p as an argument to
obj start pointing to the same Person object as depicted in the following diagram.
When you set
obj to null,
p still points to the Person object as it was before.
So you’ll get
"name=Austin, age=35" as the output. If you think about it, this is logical. When
p was passed as argument to
myMethod, the value in
p i.e. the address of Person class’ object was copied to
obj. So then both started pointing to the same Person object. When we set
p wasn’t effected. You can think of
obj as shortcut to the same text file on your computer. If you change the
obj shortcut and point it to a different file (or delete the
p will still point to the original text file. But what happens when you change the state of an object (i.e. change the values of instance fields of the object) in the called method? Lets see an example,
The output will be
"name=nothing, age=0". Some of you might be surprised, some might not? Why did this happen? The answer is simple, since both
obj were pointing to the same Person class object, so changing the state through any of the references effects the other. We know that
obj are both references that point to one object, so if we make changes to the actual object, then both the references will reflect the change. If we retake the text file shortcut example, then here
obj are shortcuts to the same text file. Making changes to the text file through any of the shortcuts will ultimately effect the text file. So after you make changes to the text file from any of the shortcuts, opening the file from the other shortcut will also show you the modified file.
So we can conclude that parameters in Java are passed by value. In case of primitives the value is copied to parameters and in case of references the address of the object is copied to method parameters.