In most of the programming languages (like C language), there are two ways of passing arguments to a method : Pass by Value and Pass by Reference.
When an argument is passed by value, the copy of the argument’s value is made and it is this copy that is passed to the method and referenced through the parameter name. As new copy is created, so any changes made to this copy does not affect the actual argument.
When an argument is passed by reference, the called method receives a reference to the original variable. Any changes made to the reference variable in the called method are automatically reflected back.
Unlike most of the programming languages, in Java, arguments are always passed to the method using a mechanism known as pass by value. A method call can pass two types of values to a method using the pass by value mechanism.
- Value of primitive type
- Value of reference type
We’ll be covering the following topics in this tutorial:
Pass By Value: Primitive Type
When a variable of primitive type(s) is passed by value, a copy is made which is passed to the parameter. Any modifications made to the primitive-type parameter(s) in the method body have no effect on the original argument in the calling method. Now let us consider the following example.
//Program to Show Passing Primitive Type as Pass By Value
class PrimitiveType
{
public static void increase(int n)
{
n += 1000;
System.out.println("Increasd Number : n " + n);
}
public static void main(String[] args)
{
int num = 10;
System.out.println("Before Increment : num : " +num);
increase(num);
System.out.println("After Increment : num : " +num);
}
}
In this example, the number entered by user is incremented by 1000. The method call, increase(num); passes a variable num of primitive type int to a method increase ().The parameter n receives a copy of the value stored in num. On increasing the value of n by 1000, this modification is not reflected back to the original variable num. So the original value of num remains unchanged.
Pass by Value: Reference Type
When you pass an object reference variable as an argument to a method, a copy of a reference to the original object is passed to the matching parameter of the method, not a copy of the object itself. Since, the reference stored in the parameter is a copy of the reference that was passed as an argument, so the parameter and argument refer to the same object in memory. Any modification made to the instance variable(s) of an object within the called method will be reflected back to the original object in the calling method as well. Now let us consider the following example.
//program to show passing object as pass by value
class Test
{
int num;
Test()
{
num= 10;
}
public void increase(Test objRef)
{
objRef.num += 1000;
}
}
class ReferenceType
{
public static void main(String[] args)
{
Test obj = new Test();
System.out.println("Before Increment : num : " + obj.num);
obj.increase(obj);
System.out.println("After Increment : num : " + obj.num);
}
}
When the reference variable obj of class type Test is passed as an argument to the method increase (), a copy of contents of obj is made and stored in parameter objRef. Both obj and obj Ref refer to the same object. When the m.un instance variable of object pointed by obj Ref reference variable is incremented by 1000 then this change is also visible to the original object obj .