In the previous tutorial, we looked at the difference between arguments and parameters in Java. Now we need to be clear about how argument values are passed to a method in
Java.
In Java, all argument values are passed to a method using only call by value (pass by value) mechanism. There is no call by reference (pass by reference) mechanism in Java. So, let's know the mechanism "call-by-value" in detail. 

Call by Value (Pass by Value) in Java


"Call by value" in Java means that argument's value is copied and is passed to the parameter list of a method. That is, when we call a method with passing argument values to the parameter list, these argument values are copied into the small portion of memory and a copy of each value is passed to the parameters of the called method. 

When these values are used inside the method either for "read or write operations", we are actually using the copy of these values, not the original argument values which are unaffected by the operation inside the method. i.e, the values of the parameters can be modified only inside the scope of the method but such modification inside method doesn't affect the original passing argument.
When the method returns, the parameters are gone and any changes to them are lost. This whole mechanism is called call by value or pass by value. Let's understand the concept "pass by value" mechanism by example program and related diagram.

Example of Call by Value in Java

Let's create an example program where we will pass argument value to the parameter of the method using call by value.
Program source code 1:
    package callbyValueExample; public class CallbyValue { int change(int b) { ++b; // Changes will be in the local variable only. return b; } public static void main(String[] args) { // Create an object of class. CallbyValue obj=new CallbyValue(); int a=20; int x=obj.change(a); System.out.println("Value of a after passing: " +a); System.out.println("Value of x after modifying: " +x); } }
    Output: Value of a after passing: 20 Value of x after modifying: 21
The below figure will illustrate how call by value works in above program.
Call by Value and Call by Reference in Java
From the above figure, you can see that when we are passing argument value to a method, first, a copy of the value is automatically created into the small portion of memory and then a copy is passed to a method by referenced using parameter name "b".  As shown in the above figure, the method change() will modify the copy of a, not the original value of "a". i.e, ++b will increment the copy of a. It will not increment the original value of a.

Thus, the value of b that will be returned, will be 21, not 20. Now, this value will be stored in the variable "x" when the return statement from the method will be executed.  However, the original value of a will remain at 20 because the modification does not affect the original argument.
Let's take another example program based on Call by value mechanism.
Program source code 2:
    package callbyValueExample; public class Test { int x=20; void modify(int x) { x=x+200; System.out.println("Value of x after modification: " +x); } public static void main(String[] args) { Test t=new Test(); t.modify(t.x); System.out.println("Original value of x: " +t.x); } }
    Output: Value of x after modification: 220 Original value of x: 20

Call by Reference (Pass by Reference) in Java


In Java "Call by Reference" means passing a reference (i.e. address) of the object by value to a method. We know that a variable of class type contains a reference (i.e. address) to an object, not object itself.


Thus, when we pass a variable of class type as an argument to a method, actually, a copy of a reference (address) to an object is passed by value to the method, not a copy of object itself because Java does not copy the object into the memory.

Actually, it copies the reference of the object into the memory and passes the copy to the parameters of the called method. If we change the reference of the object then the original reference does not get changed because this reference is not original. It's a copy.
For example:
   void m1(Emp e); where e is the object reference variable and Emp is the name of a class.
Key points:
Java passes the arguments both primitives and the copy of object reference by value. Java never passes object itself.

Let's take an example program to understand call by reference concept in Java.
Program source code 3: 
In the above program, we passed the integer values to the parameters of a method but in a real-time company project, we do not pass primitive value such as int, float, or double values to a method. We pass the reference (address) of the object as a value to the parameters of a method.

Suppose in a real-time project, we are developing a college application in which there are five modules such as Student, Library, Admin, Employee, and College. We will create a class for each module and will pass variables of class type to call the method m1() and m2() in the school class. Let's see source code.
    package callbyValueExample; public class Student { } public class Library { } public class Admin { } public class Employee { } public class College { // Declare the instance method with two objects of Student and Library classes as parameters. void m1(Student s, Library l){ // s and l are object reference variables. System.out.println("m1 is calling."); } // Similarly, public static void m2(Admin a, Employee ep){ System.out.println("m2 is calling"); } public static void main(String[] args){ // Create first an object of the class college. College c=new College(); // Now create the object of the classes Student and Library. Student s=new Student(); // (1) Library l=new Library(); // (2) // Pass object reference variables "s" and "l" as argument value to the method m1 for calling m1(). c.m1(s,l); // (3) // Above three lines of code 1, 2, and 3 can be replaced by a single line of code. Both are the same working. c.m1(new Student(), new Library()); Admin a=new Admin(); Employee ep1=new Employee(); College.m2(a,ep1); // We can pass different Employee type reference variable. Reference Variable name is not important but Employee type is important. So don't confuse in ep and ep1. // OR College.m2(new Admin(), new Employee()); } }
    Output: m1 is calling. m1 is calling. m2 is calling m2 is calling
As you can see in the above program, we have passed the reference of objects of different classes as arguments by value to call m1() and m2() methods.

Final words
Hope that this tutorial has covered almost all important points related to Call by value (Pass by value) and Call by reference (Pass by reference) in Java. I hope that you will have understood the concept call by value in Java and enjoyed them.