Table of contents
1.
Introduction
2.
Why the Confusion Around Call by Reference?
3.
Call by Value in Java
3.1.
Example of Java call by value in Java
4.
Call by Reference in Java
4.1.
Example of Call by Reference in Java
5.
Objects in Java: Reference Passed by Value
6.
Difference Between Call by Value and Call by Reference in Java
7.
Real-World Examples & Use Cases of Call by Value in Java
8.
Frequently Asked Questions
8.1.
Are call by reference and call by address the same?
8.2.
Why Call by Reference is Better?
8.3.
Is Java strictly call by value?
8.4.
How does call by value work with objects in Java?
9.
Conclusion
Last Updated: Jul 6, 2025
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

Why the Confusion Around Call by Reference?

Many developers experience confusion when it comes to method parameter behavior in Java, especially regarding call by reference. Java is strictly pass by value, but its handling of object references often creates the illusion that it's call by reference. Here's how that confusion arises:

Type 1: Object References Misunderstood as Call by Reference
In Java, when an object is passed to a method, what’s actually passed is the value of the reference to that object, not the object itself. This means the method gets a copy of the reference, not a copy of the object or a reference to the original reference. So, changes made to the object’s fields will persist, but reassigning the object itself won’t affect the original.

For example:

void updateName(Student s) {
    s.name = "Updated"; // Works
    s = new Student();  // Doesn't affect original reference
}

This misunderstanding causes call by reference confusion in Java.

 

Type 2: Behavior That Mimics Call by Reference
The illusion of Java object reference being passed by reference comes from the fact that you can modify the internal state of an object inside a method. This behavior mimics call by reference, making developers believe Java supports it.

Example:

class Student {
    String name;
}

void modify(Student s) {
    s.name = "John";
}

Student obj = new Student();
obj.name = "Alex";
modify(obj);
System.out.println(obj.name); // Output: John
You can also try this code with Online Java Compiler
Run Code


Here, it seems like the object was passed by reference because the name changed. But in reality, Java only passed the reference value, not the object or its memory address.

 This subtle behavior leads to widespread pass by value in Java confusion.

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.

Objects in Java: Reference Passed by Value

Understanding how object reference in Java works during method calls is essential for mastering Java’s parameter behavior. Many developers misunderstand Java's model, thinking it supports call by reference. In reality, Java always uses pass by value, even for objects.

Type 1: Conceptual Explanation of Reference Passed by Value
When you pass an object in a Java method, what actually gets passed is a copy of the reference to that object—not the actual object, and not the reference itself. This means the method can modify the object's internal fields, and those changes will be reflected outside the method. However, if the method reassigns the reference to a new object, that change won’t be reflected in the calling code.

This behavior is what we call reference passed by value—you can affect the object the reference points to, but not the reference itself.

 

Type 2: Code Example with Explanation

class Student {
    String name;
}

public class Test {
    static void updateStudent(Student s) {
        s.name = "John";          // modifies field
        s = new Student();        // reassignment (won't reflect)
        s.name = "Alice";
    }

    public static void main(String[] args) {
        Student stu = new Student();
        stu.name = "Alex";
        updateStudent(stu);
        System.out.println(stu.name);  // Output: John
    }
}


In this example, the method changes the name field to "John", which persists outside the method. However, the reassignment of s to a new object "Alice" has no effect on the original reference stu. This illustrates that Java passes object references by value, and only field modifications are preserved, not reference reassignments.

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.

Real-World Examples & Use Cases of Call by Value in Java

Understanding Java parameter passing is vital for writing predictable, bug-free code. Let’s explore two real-world use cases of call by value in Java to see how this concept plays out in practical development and debugging scenarios.

Type 1: Java Methods in Application Development
In real-world Java applications, it’s common to pass objects such as form inputs, configuration data, or model objects into methods. These methods often update the internal fields of the object—such changes persist because the object reference points to the same memory. However, reassigning the reference inside the method won’t affect the original object outside the method.

Example:

void updateUser(User u) {
    u.name = "Updated";       // Works
    u = new User();           // Has no effect outside
}

User u = new User();
u.name = "Original";
updateUser(u);
System.out.println(u.name); // Output: Updated


This shows how object passing in Java methods affects internal state but not the reference itself.

 

Type 2: Debugging and Side Effects of Parameter Passing
A common beginner bug in Java arises from assuming methods can swap or modify primitives directly, which isn’t possible because Java passes primitives by value. This also applies when trying to swap objects by reassigning their references—the caller remains unchanged.

Example:

void swap(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
}

int x = 5, y = 10;
swap(x, y);
System.out.println(x + ", " + y); // Output: 5, 10


Such confusion emphasizes the importance of understanding Java method behavior for better debugging and fewer side effects.

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