Is Java "pass-by-reference" or "pass-by-value"?
Java uses pass-by-value for all parameters, including both primitive data types and objects, which affects how methods behave and how they should be written.
Table of Contents
Everything in Java is “pass by value”. There is no such thing as “pass-by-reference” in Java.
Introduction
Java uses pass-by-value for all parameters, including both primitive data types and objects, which affects how methods behave and how they should be written.
For objects, the copy of the value passed is the reference of the object, not the object itself. So any changes made to the object's properties within the method or function will be reflected outside the method or function, but the reference itself is still passed by value.
You will see some examples to help illustrate these concepts and sketches.
Primitive Data type
Example 01: Increment operation with Sketch
Here is an example of pass-by-value in Java:
class Increment {
public static void main(String[] args) {
int x = 10;
increment(x);
System.out.println(x); // prints 10
}
public static void increment(int y) {
y++;
}
}
Here is a sketch that represents the pass-by-value mechanism for a primitive data type in Java:
Main Method Called Method
+--------------+ +--------------+
| x = 10 | | y = 10 |
+--------------+ +--------------+
| y++ |
+--------------+
In this example, the increment
(Called method) is called and passed the variable x
, which has a value of 10
. However, within the increment
method, the value of the local variable y
is incremented. This does not change the original variable's value x
outside of the method. So when the statement System.out.println(x);
is executed, it will still print "10".
This sketch illustrates the pass-by-value mechanism for primitive data types in Java, where a copy of the argument's value is passed to the method. Any changes made to the argument within the method will not affect the original argument in the calling method.
Example 02: Change value
Here's another example to help illustrate the concept of "pass-by-value" with primitive data types in Java:
class Example {
public static void main(String[] args) {
int num = 5;
System.out.println("Before method call, num: " + num);
changePrimitive(num);
System.out.println("After method call, num: " + num);
}
public static void changePrimitive(int n) {
n = 10;
}
}
In this example, we create a primitive integer num
and set its value to 5. Then, we pass the num
value to the changePrimitive
method.
In the changePrimitive
method, we change the value of the n
parameter to 10. However, this change does not affect the value of num
in the calling method.
The output of the code will be:
Before method call, num: 5
After method call, num: 5
This demonstrates that in Java, primitive data types are passed by value, so changes made to the parameters within a method are not reflected in the calling method.
Objects
Example 01: Animal Object
When an object reference is passed as an argument to a method, the method receives a copy of the reference but not the object itself. The method operates on the reference to the object, not the object itself, which remains in memory.
Any changes made to the object through the reference within the method will be reflected in the calling method. However, the reference itself is passed by value, so the method cannot change the reference in the calling method to refer to a different object.
Here's an example to help illustrate the concept of "pass-by-value" in Java:
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
class Example {
public static void main(String[] args) {
Person person = new Person("Patrick");
updateName(person);
System.out.println(person.getName()); // prints "Jane"
}
public static void updateName(Person person) {
person.setName("Jane");
}
}
In this example, we create an instance of the Person
class and call it person
. The name of the person
instance is set to "Patrick"(from "The Mentalist" TV Show for fun 😊). Then, we pass the person
reference to the updateName
method.
In the updateName
method, we use the reference to call the setName
method and change the name of the person
instance to "Jane". Since the method operates on the reference to the person
object, the changes made to the name are reflected in the calling method, so the output of the code will be:
Before method call, dog name: Patrick
After method call, dog name: Jane
This demonstrates that objects are passed by reference in Java, but the references themselves are passed by value.
2. Example#1 - Integer Array
Another example of pass-by-value in Java is when an array is passed as an argument to a method. The array reference is passed by value, meaning that a copy of the reference to the array is passed to the method. Any changes to the array elements within the method will be reflected outside the method, but the reference itself is still passed by value. Here is an example:
import java.util.Arrays;
public class IntegerArray {
public static void main(String[] args) {
int[] numbers = {1, 2, 3};
updateArray(numbers);
System.out.println(Arrays.toString(numbers)); // prints [4, 2, 3]
}
public static void updateArray(int[] array) {
array[0] = 4;
}
}
In this example, the updateArray
method is called and passed the array numbers
. Within the method, the first element of the array is updated to the value 4. This change is reflected outside the method, and when the statement System.out.println(Arrays.toString(numbers));
is executed, it will print "[4, 2, 3]".
Conclusion
In conclusion, it is important to understand the difference between "pass-by-value" and "pass-by-reference" in Java, as it affects the behavior of arguments passed to methods.
In Java, primitive data types are passed by value and objects are passed by reference(the reference itself is still passed by value). This means that changes made to primitive data type arguments within a method will not affect the original values.
In contrast, changes made to objects through their references within a method will be reflected in the calling method, but the reference itself is still passed by value.
Gopi Gorantala Newsletter
Join the newsletter to receive the latest updates in your inbox.