Table of contents
1.
Introduction
2.
Call by Value in Java
2.1.
Example of Java call by value in Java
3.
Call by Reference in Java
3.1.
Example of Call by Reference in Java
4.
Difference Between Call by Value and Call by Reference in Java
5.
Frequently Asked Questions
5.1.
Are call by reference and call by address the same?
5.2.
Why Call by Reference is Better?
5.3.
Is Java strictly call by value?
5.4.
How does call by value work with objects in Java?
6.
Conclusion
Last Updated: Dec 6, 2024
Easy

Call by Value and Call by Reference in Java

Author Saksham Gupta
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, understanding how method arguments are passed is essential for writing efficient and bug-free code. Java supports two primary methods for passing arguments to functions: Call by Value and Call by Reference. These two mechanisms determine how data is passed to methods and how changes to the data inside methods affect the original variables. While Call by Value involves passing a copy of the argument's value, Call by Reference works by passing the reference (or memory address) of the argument.

Call by Value and Call by Reference in Java

Recommended topic- Iteration Statements in JavaDuck Number in Java

Call by Value in Java

The values of actual parameters are copied to the function's formal parameters in this parameter passing mechanism, and the two types of parameters are stored in different memory locations. As a result, any changes made inside functions are not reflected in the caller's actual arguments. Let's understand this better by the example.

Example of Java call by value in Java

class Tester{
   public static void main(String[] args){
      int first = 6;
      int second = 9;
      System.out.println("Before function call, first = " + first + " and second = " + second);
      /* Function will be called*/
      Function(first, second);
      System.out.println("After function call , first = " + first + " and second is " + second);
   }
   public static void Function(int first, int second) {
      System.out.println("Before Function calling the values are (Inside), first = " + first + " second = " + second);
      int temp = first;
      first = second;
      second = temp;
      System.out.println("After operations in function (Inside), first = " + first + " second = " + second);
   }
}
You can also try this code with Online Java Compiler
Run Code

 

Output

Before function call, first = 6 and second = 9
Before Function calling the values are (Inside), first = 6 second = 9
After operations in function (Inside), first = 9 second = 6
After function call , first = 6 and second is 9

Explanation

Now let's understand each line of the above output.

First, we can see that before the function call, values of variables ‘first’ and ‘second’ will get printed, i.e., 6 and 9.

Second, When we go inside the function, we print the values of ‘first’ and ‘second’. Now we have not made any changes here, so the output would still be the same, i.e., 6 and 9.

Now we are swapping values of ‘first’ and ‘second,’ and then we are printing; thus, the output would be 9 and 6.

Now we have made changes in the copy variables that were created for the function ‘Function’ thus the original values of ‘first’ and ‘second’ will remain unchanged and our output would be 6 and 9.

Call by Reference in Java

Now in the Call by reference method, both the actual and formal parameters correspond to the identical locations. Thus, any changes performed inside the function are reflected in the caller's actual parameters. Only call by value is used in Java while passing reference variables as well. It makes a duplicate of the references and provides them to the methods as useful. Creating a copy of a reference is harmless because it points to the same object's address. However, if a new item is added to the reference list, it will not be reflected. Let's understand this better by the example.

Example of Call by Reference in Java

class Testing {
   public static void main(String[] args) {
      Helper first = new Helper(6);
      Helper second = new Helper(9);
      System.out.println("Before function call, first = " + first.variable + " and second = " + second.variable);
   
      /* Function called */
      Function(first, second);
      System.out.println("After function call , first = " + first.variable + " and second is " + second.variable);
     
   }
   public static void Function(Helper first, Helper second) {
      System.out.println("Before Function calling the values are (Inside), first = " + first.variable + " second = " + second.variable);
 
      Helper temp = new Helper(first.variable);
      first.variable = second.variable;
      second.variable = temp.variable;
      System.out.println("After operations in function (Inside), first = " + first.variable + " second = " + second.variable);
   }
}
class Helper {
   public int variable;
   public Helper(int variable){ this.variable = variable;}
}
You can also try this code with Online Java Compiler
Run Code

 

Output

Before function call, first = 6 and second = 9
Before Function calling the values are (Inside), first = 6 second = 9
After operations in function (Inside), first = 9 second = 6
After function call , first = 9 and second is 6


You can also find the output of this java compiler code here.

 

Explanation

Now let's understand each line of the above output.

First, we can see that before the function call, values of variables ‘first’ and ‘second’ will get printed, i.e., 6 and 9.

Second, When we go inside the function, we print the values of ‘first’ and ‘second.’ Now we have not made any changes here, so the output would still be the same, i.e., 6 and 9.

Now we are swapping values of ‘first’ and ‘second,’ and then we are printing; thus, the output would be 9 and 6.

Here comes the major difference. After we have exited from function ‘Helper’, we again print values of ‘first’ and ‘second,’ Here, we see the output is 9 and 6 instead of 6 and 9. This is because in call by reference, the values get updated in original variables instead of creating their copy, So once we have swapped the values of ‘first’ and ‘second’ inside the ‘Helper’ function, values in original variables will also get changed.

Difference Between Call by Value and Call by Reference in Java

Feature

Call by Value

Call by Reference

DefinitionA method receives a copy of the value passed to it.A method receives the actual reference of the object, allowing changes to affect the original.
Java SupportJava uses call by value for both primitives and objects.Java does not support true call by reference. It passes the reference by value.
Effect on Original DataChanges to the value inside the method do not affect the original variable.Changes to the object's state inside the method affect the original object (but not the reference itself).
PrimitivesOnly a copy of the primitive value is passed. Changes are local to the method.Not applicable, as Java uses call by value for primitives.
ObjectsA copy of the reference to the object is passed. Modifications to the object’s attributes affect the original object.Not applicable, as Java does not support true call by reference.
Example with PrimitivesIf an int is passed, modifying it inside the method does not affect the original int.Not applicable.
Example with ObjectsIf an object is passed, modifying its fields will affect the original object, but reassigning the reference won’t.Not applicable.
MemoryRequires more memory as a copy of the variable is created.Would modify the original object directly, but Java only passes references by value.

Frequently Asked Questions

Are call by reference and call by address the same?

Call by reference and call by address are similar concepts, but in call by reference, a reference to the actual variable is passed, whereas in call by address, the memory address of the variable is passed.

Why Call by Reference is Better?

Call by reference is often better for performance as it avoids copying large amounts of data, allowing functions to modify objects directly, making it ideal for situations where changes to the original object are required.

Is Java strictly call by value?

Yes, Java is strictly call by value. However, it may seem like call by reference when passing objects. In Java, the value passed for objects is the reference (memory address), but it still behaves like passing a value.

How does call by value work with objects in Java?

In Java, when objects are passed to methods, the reference to the object is passed by value. This means the method can modify the object’s internal state, but cannot change the reference to point to a new object.

Conclusion

In Java, call by value is used for both primitives and objects. For primitives, a copy of the value is passed, and changes don’t affect the original variable. For objects, the reference is passed by value, allowing changes to the object's internal state but not its reference. Java does not support true call by reference like some other languages.

Explore more:

Live masterclass