This is one of the most frequently asked interview questions for experienced Java developers. In this post, we'll understand the concepts of pass-by-value and pass-by-reference, and explain how Java handles method arguments.
Pass-by-value in Java means that a copy of the value is passed to the method. Pass-by-reference means that the memory address (reference) of the variable is passed. In Java, all method arguments are passed by value.
Pass-by-value in Java means that a copy of the value is passed to the method. Pass-by-reference means that the memory address (reference) of the variable is passed. In Java, all method arguments are passed by value.
For primitive data types, Java passes a copy of the actual value. For objects, Java passes a copy of the object reference, not the object itself. As a result, both the original reference and the copied reference point to the same object in memory. Therefore, changes made to the object's state inside the method are reflected outside the method. However, reassigning the object reference inside the method does not affect the original reference.
Read Top Java Blogs - Feedspot Top 40 Java Blogs
Is Java call by value or call by reference ?
Java doesn't pass method arguments by reference; it passes them by value. So Java uses or supports call by value.
In call by value, the modification done to the parameter passed does not reflect in the caller's scope while in the call by reference, the modification done to the parameter passed are persistent and changes are reflected in the caller's scope.
Is Java call by value or call by reference ?
Java doesn't pass method arguments by reference; it passes them by value. So Java uses or supports call by value.
In call by value, the modification done to the parameter passed does not reflect in the caller's scope while in the call by reference, the modification done to the parameter passed are persistent and changes are reflected in the caller's scope.
The below example explains the call by value and call by reference with output.
Call by value example:-
The following code shows an example of call by value. The values of the arguments will remain the same even after the method invocation.
PassByValueExample.java,
Call by value example:-
The following code shows an example of call by value. The values of the arguments will remain the same even after the method invocation.
PassByValueExample.java,
package com.test;
public class PassByValueExample {
public static void main(String[] args) {
int x = 10;
System.out.println("Value of x before calling method changeValue() - " +x);
changeValue(x);
System.out.println("Value of x after calling method changeValue() - " +x);
}
public static void changeValue(int x) {
x = 12;
System.out.println("Value of x inside method - "+x);
}
}
Output:-
Value of x before calling method changeValue() - 10
Value of x inside method - 12
Value of x after calling method changeValue() - 10
In the above output, the value of x after invoking the method, will remain same, it doesn't impact on original value.
Call by reference example:-
The following code is the example of call by reference where List reference passed as an argument of method. The elements of list will get change when we modify the element in the calling method.
PassByReferenceExample.java
package com.test;
import java.util.ArrayList;
import java.util.List;
public class PassByReferenceExample {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("A");
System.out.println("List before calling method - "+list);
addElement(list);
System.out.println("List after calling method - "+list);
}
public static void addElement(List<String> list) {
list.add("B");
System.out.println("List elements inside method - "+list);
}
}
Output:-
List before calling method - [A]
List elements inside method - [A, B]
List after calling method - [A, B]
Related Posts:-
1) String Interview Questions and Answers
2) Exception Handling Interview questions and answers
3) Difference between Loose Coupling and Tight Coupling in Java With Examples.
4) Interface and Abstract Class Interview Questions and Answers
No comments:
Post a Comment