Java is a strictly pass-by-value language, which means that when values are passed to methods or functions, they are copied and passed to the method rather than being directly referenced. To illustrate this, I will provide relevant examples.
public class PassByValueExample { public static void main(String[] args) { int x = 5; System.out.println("Before calling modify() method: x = " + x); modify(x); System.out.println("After calling modify() method: x = " + x); } public static void modify(int num) { num = 10; System.out.println("Inside modify() method: num = " + num); } } |
Explanation:
In this program, we have a variable x with an initial value of 5. We pass this value to the modify() method. Inside the modify() method, we change the value of the num parameter to 10.
However, when we print the value of x again after calling the modify() method, we can see that it is still 5. This is because Java is strictly pass-by-value, and in this case, the value of x is copied and passed to the num parameter. So, modifying the value of num does not affect the original value of x.
The output of this program will be:
Before calling modify() method: x = 5
Inside modify() method: num = 10
After calling modify() method: x = 5
As you can see, even though the num value inside the modify() method is changed to 10, the value of x remains unchanged outside the method.
class Sample { int value; Sample(int value) { this.value = value; } } public class PassByReferenceExample { public static void main(String[] args) { Sample e = new Sample(5); System.out.println("Before calling change() method: e.value = " + e.value); change(e); System.out.println("After calling change() method: e.value = " + e.value); } public static void change(Sample s) { s = new Sample(10); System.out.println("Inside change() method: s.value = " + s.value); } } |
Explanation:
In this program, we have a class Sample with an int member variable value. We create an instance of Sample called e with an initial value of 5.
When we pass e to the change() method, it is not e itself that is passed, but a copy of the reference to the same Sample object. Inside the change() method, we assign a new Sample object with a value of 10 to the s parameter.
However, when we print the value of e.value again after calling the change() method, we can see that it is still 5. This is because the s parameter inside the change() method is a copy of the reference, and reassigning it to a new Sample object does not affect the original reference e.
The output of this program will be:
Before calling change() method: e.value = 5
Inside change() method: s.value = 10
After calling change() method: e.value = 5
As you can see, even though the s.value inside the change() method is changed to 10, the value of e.value remains unchanged outside the method.
class Sample { int value; Sample(int value) { this.value = value; } } public class PassByReferenceExample { public static void main(String[] args) { Sample p = new Sample(5); System.out.println("Before calling change() method: p.value = " + p.value); change(p); System.out.println("After calling change() method: p.value = " + p.value); } public static void change(Sample s) { s.value = 10; System.out.println("Inside change() method: s.value = " + s.value); } } |
Explanation:
In this program, we have a class Sample with an int member variable value. We create an instance of Sample called p with an initial value of 5.
When we pass p to the change() method, it is not p itself that is passed, but a copy of the reference to the same Sample object. Inside the change() method, we modify the value of the s.value member variable directly.
When we print the value of p.value again after calling the change() method, we can see that it is now 10. This is because both the original reference p and the copy s point to the same object in memory. Therefore, any changes made to the object through s are reflected in p as well.
The output of this program will be:
Before calling change() method: p.value = 5
Inside change() method: s.value = 10
After calling change() method: p.value = 10
As you can see, the modification of the s.value inside the change() method is reflected in the original reference p, and its value becomes 10.
In conclusion, Java is strictly pass-by-value or, more accurately, pass-by-copy-of-reference. When primitive types are passed as arguments to methods, copies of the values are made and passed to the methods, which means modifications inside the methods do not affect the original values.
When it comes to objects and references, a copy of the reference to the object is made and passed to methods. This copy still points to the same object in memory, so modifications to the object’s content are reflected in both the original reference and the copy. However, the actual reference itself is not changed or affected.
Therefore, while Java is not strictly pass-by-reference, it exhibits behavior that may appear similar due to the manipulation of references. However, it is important to note that the original reference is not altered.
In summary, Java is strictly passed by value or pass-by-copy-of-reference.
Thank you!
Leave a Reply