In the Java programming language, Call by Value and Call by Reference are terms used to describe the way in which arguments are transferred to methods.
Call by Value in Java
In Java, the act of passing a value as an argument when calling a method is known as call by value. In Java, this involves passing a duplicate of the variable to the method. Consequently, any changes made within the method are limited to that specific method and do not alter the original variable in the main method.
The key difference lies in how Java handles call-by-reference and call-by-value functions. When passing a variable with primitive data types, it is treated as call by value. Consequently, any changes to the variable within the function do not affect the caller's scope.
Important Points to Remember
- Java always uses call by value, even for objects.
- When a primitive type (like int, double, float) is passed to a method, a copy of the value is made.
- Changes inside the method do not affect the original variable
Example of Call by Value
When using call by value, the original value remains unchanged. To illustrate this concept, consider the following basic example:
Example
class Main {
int data=50;
void change(int data) {
data=data+100; //changes will be in the local variable only
}
public static void main(String args[]) {
Main op=new Main ();
System.out.println("before change "+op.data);
op.change(500);
System.out.println("after change "+op.data);
}
}
Output:
before change 50
after change 50
Another Example of Call by Value in Java
When using the call by reference approach, any modifications made within the called method will affect the original value. This is particularly true when passing an object rather than a primitive data type, as it leads to changes in the original value. In the provided illustration, an object is being passed as a value.
Example
class Main {
int data=50;
void change(Main op) {
op.data=op.data+100; //changes will be in the instance variable
}
public static void main(String args[]) {
Main op=new Main();
System.out.println("before change "+op.data);
op.change(op); //passing object
System.out.println("after change "+op.data);
}
}
Output:
before change 50
after change 150
Call by Reference (Object Reference) in Java
In Java, the concept of Call by Reference involves passing a reference, which is the memory address of the object, by value to a method. Despite Java's adherence to call by value, when providing an object's reference, it duplicates the reference before passing it as a value to the method. The key distinction between call by value and call by reference in Java lies in the fact that the duplicated reference still directs to the original address, guaranteeing that any alterations are mirrored in the main method.
Important Points to Remember
- Java does not support true call by reference; it passes the reference by value.
- When an object is passed, a copy of the reference is made.
- The reference still points to the same object, so modifications inside the method affect the original object.
Example of Call by Reference
Example
class Data {
int value;
}
public class Main {
public static void main(String[] args) {
Data obj = new Data();
obj.value = 10;
modifyObject(obj);
System.out.println("After the method call, value = " + obj.value); // Modified to 15
}
//passing object as parameter
static void modifyObject(Data d) {
d.value = d.value + 5; // Changes the actual object
}
}
Output:
After the method call, value = 15
Differences Between Call by Value and Call by Reference
| Feature | Call by Value | Call by Reference |
|---|---|---|
| Definition | A copy of the actual value is passed to the method. | A copy of the reference (address) is passed to the method. |
| Java Support | Java uses call by value for both primitive types and objects. | Java does not support true call by reference; instead, it passes the reference by value. |
| Effect on Original Data | Changes made to the value inside the method do not affect the original variable. | Changes to the object's state within the method impact the original object (but not the reference itself). |
| Applicable to | It is applicable to primitive types (like int, double, float, etc.) only. | It is applicable to objects ( instances of classes) only. |
| Objects | A copy of the reference to the object is passed. Changes to the object's attributes affect the original object. | Not applicable, as Java does not support true call-by-reference. |
| Example with Primitives | If an int is passed, modifying it inside the method does not impact the original int. | Not applicable. |
| Example with Objects | If an object is passed, modifying its fields will affect the original object, but reassigning the reference will not. | Not applicable. |
| Memory Consumption | It requires more memory because a copy of the variable is created. | The reference points to the same memory location as the original object, so no more memory is needed. |
| True call by Reference? | Yes | No |
Java call by value and call by reference MCQ
- In Java, how are primitive data types passed to methods?
- By reference
- By Value
- By pointer
- By object
In Java, when primitive data types are passed to methods, they are passed by value. This means that a duplicate of the value is passed, and any changes made to the parameter inside the method do not impact the original variable.
- What will be the output of the code snippet provided below?
public class Test {
public static void main(String[] args) {
int a = 10;
modify(a);
System.out.println(a);
}
static void modify(int x) {
x = 20;
}
}
- Compile-time error
Explanation: The variable a is passed by value to the modify method, so the original value of a remains unchanged, and the output is 10.
- How are objects passed to methods in Java?
- By reference
- By value of the reference
- By copy of the object
- By memory address
In Java, when passing objects as arguments, the value of the reference is actually passed. This implies that a copy of the reference to the object is made, but both the original reference and the copied reference still point to the same object stored in memory.
- What result will the code below produce?
class Test {
int data = 50;
void change(Test obj) {
obj.data = obj.data + 100;
}
public static void main(String args[]) {
Test t = new Test();
t.change(t);
System.out.println(t.data);
}
}
Explanation: The method change modifies the data field of the Test object. Since the object reference is passed by value and points to the same object, the change is reflected in the original object, and the output is 150.
- Which of the following statements is true regarding call by value and call by reference in Java?
- Java supports call by reference for both primitives and objects.
- Java supports call by value for primitives and call by reference for objects.
- Java supports call by value for both primitives and objects.
- Java supports call by reference for primitives and call by value for objects.
In Java, the method of passing arguments is call by value, applicable to both primitive data types and objects. When objects are involved, the reference value pointing to the object is actually passed, enabling methods to alter the object's state.