Call by Value and Call by Reference in Java

In Java programming language, method can be called in two ways, which is known as Call by Value and Call by Reference. These two ways differentiate by the type of values passed to method as parameters.

In Java, primitive data types are passed as call by value and non-primitive types are always passed as references to a method.

The parameters passed to a method are called actual parameters whereas the parameters received by a method are called formal parameters.


Call by Value

Call by value method copies the value of an argument into the formal parameter of that function. The two types of parameters are stored in different memory locations. Therefore, changes made to the parameter inside functions body are not reflected in actual parameters of the caller.

Java program using call by value.

public class CallByValue {
    private static void increment(int val) {
        val++;
        System.out.println("Inside method");
        System.out.println("Value = " + val);
    }

    public static void main(String[] args) {
        int num = 10;
        System.out.println("Before calling: " + num);
        increment(num);
        System.out.println("After calling: " + num);
    }
}
Output
Before calling: 10
Inside method
Value = 11
After calling: 10

In above java program, we are calling "increment" method by passing an integer variable num as parameter using call by value. The value of variable num is copied into formal parameter val.

The values of num and var variable is stored at different memory locations. Hence, incrementing the value of parameter var inside "increment" method will not change the value of num.


Call by Reference

Call by reference method copies the address of an argument into the formal parameter. In this method, the address is used to access the actual argument used in the function call. Both the actual and formal parameters refer to the same memory locations. Any changes made to the parameter inside the function body are actually reflected in actual parameters of the caller.

Java program using call by reference.

class Length {
    int length;

    Length(int length) {
        this.length = length;
    }

    public void print() {
        System.out.println("Length= " + this.length);
    }
}

public class CallByReference {
    private static void increment(Length lengthArg) {
        lengthArg.length = lengthArg.length + 1;
    }

    public static void main(String[] args) {
        Length lengthObject = new Length(10);
        // Before increment call
        lengthObject.print();
        // Call increment method using
        // call by reference
        increment(lengthObject);
        // After increment call
        lengthObject.print();
    }
}
Output
Length= 10
Length= 11

In above java program, we are calling "increment" method by passing object reference as parameter using call by reference.

Both lengthObject and lengthArg pointing towards same object at the same memory location. Hence, incrementing lengthArg inside "increment" method will change the value of lengthObject.


Difference between Call by Value and Call by Reference

Call by Value Call by Reference
A copy of the variable is passed to method. A reference to a variable is passed to method.
Actual and formal arguments will be created in different memory locations. Both actual and formal parameters refer to the same memory locations.
Original parameter value is not modified by the changes inside method body. The original parameter value gets modified by the changes inside method body.
Values of variables are passed by the simple technique. Pointer/reference variables are necessary to define to store the address values of variables.