JB Header
Java Parameter Passing is 'Pass By Value' Or 'Pass By Reference'
This article delves into the topic of whether java has parameter passing by value or reference.
Parameter passing basics There are two ways in which parameter values are passed around in programming languages -
  • Pass By Value: Here a copy of the parameter value is made when it is passed between methods. I.e a new variable is created in memory in the called method which has scope local to that method. Any change done to the variable value in called method has no bearing on the variable value in the original calling method.
  • Pass by Reference: Here a reference to memory location where the variable is stored is passed to the called method. So, the called method and the calling method both operate on the same variable copy. I.e. any changes done on the variable in either the calling or the called method reflects in both methods.
Parameter passing in Java In java both object references as well as primitive data types are passed by value. I.e. Java by definition is "Pass By Value".

A question which pops up in a programmer's mind immediately is that passing of parameters by value seems fine for primitives for int, long, String etc. But how does parameter passing work in cases where an object instance is passed as a parameter between methods?
The answer is - in case of passing of object references the references to objects are passed by value. I.e. if one alters an object's instance variable in called method then it gets changed in calling method as well because both the methods have a reference to the same object in their possession.

To summarize, the appropriate explanation for parameter passing in Java is that - Java passes around all parameters by value and, specifically in case of objects being passed as parameters, its actually object references which are passed by value.