Pass by Value vs Pass by Reference
In Java function programming, actual and formal variable arguments declared in a user-created function program are passed to function methods using pass-by-value and pass-by-reference. Actual arguments are passed as formal method arguments. Pass-by-value and pass-by-reference describe how modifications to the inner variables of a user-declared function method directly or indirectly impact the outer variables of the function method.

Pass by value reference in Java – concept.
- In Java programming, the pass-by-value reference method is used by default to pass variable arguments to user-declared function methods. However, whether and how value arguments are passed to a function method depends on the type of the function data type being passed or used.
- Primitive data types (e.g., int, double, char) – When a primitive data type value is passed to a function method in a Java program, a copy of the function argument value is created. Modifications made to the declared variable parameter argument inside a user function method do not affect the original value outside the method.
- Object references (e.g., string, array, custom object) – When a string, array, or custom object value is passed to a user-declared function method, a copy of the declared function object reference is created during this process, as if it were a real object. This means that the function object method has direct access to the original object. Any modifications made to the current state field/attribute of the used string, array, or custom object do not directly affect the original object. As in, if the object reference is reassigned inside the function method, then the declaration of the function method has no special effect on the original reference.
Passing a value with a primitive data type in Java programming.
Example of a primitive data type value in a Java function.
public class Main
{
public static void editedValue(int integer) {
integer = 1; // Here we modify the local copy of the variable
System.out.println(“\n inner side value of the function method – ” + integer);
}
public static void main(String[] args) {
int p = 2;
editValue(p); // Here it passes a copy of the p variable
System.out.println(“\n outer side value after the function method call – ” + p); // Here the p variable value remains 2
}
}
Explaining primitive data type value.
- Here, in this program, the variable p is passed as a value, which means that a copy of the variable p is created when the function method editedValue(p) is called.
- Here, the integer variable declared inside the editedValue() function method is replaced by a local copy, but this process does not affect the variable p in the main function method.
Primitive data type value conclusion.
- Pass by Value – For primitive data types in function methods, only a copy of the declared variable value is passed, and the original variable value remains unchanged during this process.
Pass by Value References with Objects in Java Function Methods.
The Pass by Value method is used to pass user-declared function arrays, strings, or custom objects to a Java program. The passed function method object is the reference address of the value object, not the actual object itself. This function generates call-by-reference behaviour in the background of the program, which behaves like Pass by Reference in the program, but in reality, it is still similar to the Pass by Value method.
Example of the Pass by Value object method in a Java program.
class Employee {
String emp_name;
Employee(String emp_name) {
this.name = emp_name;
}
}
public class {
public static void modifyName(Employee e) {
e.name = “Bhavishi”; // here it changes the name of the function object
System.out.println(“\n here inner function method – ” + e.name);
}
public static void main(String[] args) {
Employee employee = new Employee(“Harry”);
modifyName(employee); // here it passes the reference to employee function method
System.out.println(“\n here After function method call – ” + employee.name); // here employee name is modified with “Bhavishi”
}
}
Explaining the Pass by Value Object Method.
- In this program, the variable employee declared is a function method object. When Java users pass it to the function method (e.g., modifyName(Employee e), they are passing the variable reference to the object, not the object itself.
- Within the function method, Java users can modify the object’s state, such as its fields. Because the original object reference in the main function and the reference in modifyName both point directly to the same object, changes made to the object’s name field are visible outside the function method.
- For example, if Java users modify the object reference themselves (e.g., e = new Employee(“Siddhi”)), this has no direct impact on the original variable reference outside the function method.
Result of the Pass by Value Object Method.
Pass by value with objects – When a declared object value is passed into a Java function method, the object’s reference is passed by value. This process allows Java users to modify the state and fields of the declared array, string, or custom object. However, if Java users reassign a reference to point to a new reference object inside the function method, it has no effect on the original reference value outside the function method.
Main Differences among Pass by Value and Pass by Reference in function method
| Feature | Pass by Value function method | Pass by Reference function method |
| What is passed? | In pass by value function method, A copy of the parameter variable value for primitives’ data type or the reference for objects is passed. | In the pass by reference function method, the actual reference to the array, string, or custom data object the address in memory is passed. |
| Impact on Primitive Types | In pass by value function method, the original declare variable value is not affected during process. Any custom changes inside the function method do not direct impact on the original variable. | pass by reference function method, do not impact primitive data type behaviour, because it works on object array, string or custom data types only. |
| Impact on Objects | In pass by value function method, the reference to the object is passed by value. When modifying the object’s state, it will direct affect the original object, but when you reassigning it, the reference does not affect the original reference impact. | In the pass by reference function method, when the Modifying the object via the reference affects the original object value, and when you reassigning the reference will affect the caller as well. |
| Java process | In pass by value function method, it Always pass by value default in Java function method program, it depends on the whether the parameter is a primitive or an object data type reference. | pass by reference function method, this is not directly supported in Java object data type, but Java does not have pass-by-reference for primitives or object references data types. |
Conclusion of Pass by Value vs. Pass by Reference.
- Java function methods mostly use pass-by-value references, while the value of the variable passed into the function method can vary depending on its use. For example, if Java programmers are working with primitive data types, or with reference objects.
- For primitive types in Java programming, Java passes a copy of the variable’s value. Modifications made within a user-declared function method have no direct impact on the original variable.
- For Java object references, a copy of the declared array, string, or custom object reference variable address is passed to the Java function method. This process allows modifications to the current state of the custom object. Subsequent modifications to the function method’s outer reference are not permitted.
- This behaviour uses the pass-by-reference method when working with custom Java objects. However, in reality, this process still follows the pass-by-value principle of object references.
