1. Overview

Different programming languages handle argument passing differently but in java, the arguments are passed by value only. 

When an argument is passed in java a copy of that argument is created in the stack and then it is passed to that method. It means that the method to which arguments are passed has different variables.

2. Primitive variables

When the primitive variables are passed as arguments, new variables with the same values are created and then those variables are used by the called method.

Let's understand this by an example:

class ArgumentTest {
    public static void main(String arr[]) {
        int num1 = 4;
        int num2 = 9;
        System.out.println("Before swapping : ");
        System.out.println("num1 = " + num1 + ", num2 = " + num2);
        swap(num1, num2);
        System.out.println("After swapping : ");
        System.out.println("num1 = " + num1 + ", num2 = " + num2);
    }

    // Method to swap the values
    public static void swap(int x, int y) {
        int temp = x;
        x = y;
        y = temp;
    }
}

Output:

Before swapping : 
num1 = 4, num2 = 9
After swapping :  
num1 = 4, num2 = 9

In the above code, we created two int variables and then we called the swap() method to swap the values of the variables by passing them as the arguments to the swap() method.

But as you can see the values of the variables are not swapped. Why is that?

2.1. Under the hood

Saying that the variables did not swap is entirely correct because variables did swap but not the ones we are printing. The variables that are swapped are the variables of the swap() method. Because swap() was working on the copies of num1 and num2 so it swapped their copies instead of the original ones in the main() method. You can confirm it by printing the values of x and y in the swap method.

    public static void swap(int x, int y) {
        System.out.println("Values of x and y in swap method before swapping");
        System.out.println("x = " + x + ", y = " + y);
        int temp = x;
        x = y;
        y = temp;
        System.out.println("Values of x and y in swap method after swapping");
        System.out.println("x = " + x + ", y = " + y);
    }

Output:

Before swapping :
num1 = 4, num2 = 9
Values of x and y in swap method before swapping
x = 4, y = 9
Values of x and y in swap method after swapping
x = 9, y = 4
After swapping :
num1 = 4, num2 = 9

As you can see the values of x and y in swap() method did swap.

Look at the below figure to understand a little better how variables were created and swapped.

Frame 1.png

The variables num1 and num2 were never swapped only their copies x and y were swapped.

So how do we swap the num1 and num2?

We can swap the variables by using reference variables instead of primitive variables. Let's take a look at passing reference variables in java.

3. Reference variables

In java "Reference variables" are also passed by value, a duplicate of the reference variable is created in the stack that is used by the called method, but the difference is that both the reference variables point to the same object which is stored in the heap.

Now to create a reference variable we have to create a class that contains an int variable.

class Number {

    int value;

    public Number(int value) {
        this.value = value;
    }
}

Now we will pass the reference of this Number class to the swap method.

class ArgumentTest {
    public static void main(String arr[]) {
        Number num1 = new Number(4);
        Number num2 = new Number(9);
        System.out.println("Befor swapping");
        System.out.println("num1 = " + num1.value + " num2 = " + num2.value);
        swap(num1, num2);
        System.out.println("After swapping");
        System.out.println("num1 = " + num1.value + " num2 = " + num2.value);
    }

    public static void swap(Number x, Number y) {
        int temp = x.value;
        x.value = y.value;
        y.value = temp;
    }
}

Output:

Befor swapping
num1 = 4 num2 = 9
After swapping
num1 = 9 num2 = 4

As you can see the values of num1 and num2 actually swapped this time.

Because the duplicate variables also pointed to the same objects as the original variables, when we swapped the values of x and y the values of both the objects in the heap were also swapped.

Take a look at what happened in the memory:

Frame 2.png

Instead of swapping the values of the duplicate variables this time the values of the objects towards which they were pointing were swapped.

4. Summary

In this quick tutorial, we saw how the argument passing mechanism works in java and how different types of parameters are handled by java.