This also one of the important question for experienced Java developers. In this post, we need understand what is call by value(pass by value) and call by reference
Call by value in java means passing a copy of the value to be passed. Pass by reference in java means the passing the address itself. In Java the arguments of the method are always passed by value.
With Java objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same Java object. Java primitives too are passed by value.
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. 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 in java means passing a copy of the value to be passed. Pass by reference in java means the passing the address itself. In Java the arguments of the method are always passed by value.
With Java objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same Java object. Java primitives too are passed by value.
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. 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,
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