Call by Value and Call by Reference in java with real-world examples

Call by Value and Call by Reference in java with real-world examples

Hello Readers, Welcome back again with this new tutorial in Java. In this tutorial, we will learn Call by Value and Call by reference in depth with good programs and examples. And Belief Me at the end of this tutorial your doubt related to call by value and call by reference will be cleared. Along with it, we will also see many real-world examples of call by value and call by reference in Java.

You can also read my new powerful blog:-

dynamic method dispatch in java
upcasting and downcasting in java
difference between throw and throws in java
advantages of oops in java

So Lets start, Call by Value and Call by reference is a very useful and logical concept of Java. Basically Java doesn’t have a call by reference concept like C as C has a pointer concept. So here we pass the object as a reference to implement the call by reference in Java.

Lets see the real world example to understand this concepts.

Real-world example to understand call by value in Java

As we know that what is an actual argument and formal argument. Because to understand this concept you should aware of the actual and formal arguments of any method.

So In call by value actual argument gets not affected if we apply any operation on its formal arguments.

Suppose a father has given 50 rupees to his son. Now, the father gets not affected whatever son does with that 50 rupees.

We can see the below example to understand it

[java]
int a, b=10;
a=b;
a=a+20
[/java]

The above example will give you great insights into the call by value example. As we have initialized b with value 10. Now we have assigned the same value in variable a. Now we have manipulated the value of a by adding 20. By doing this original value of b will be not changed and it will remain the same.

Real-world example to understand call by reference in Java

In call by reference, we do not pass the exact value. Instead of the value, we pass the original address so the if we did changes anywhere value gets changed at the origin.

To understand this concept bank is very good example. Suppose you have a bank account and debit card.

Now either you withdraw money from the bank by showing your passbook and signature or you withdraw money from your debit card. Your account will get updated with a new balance.

Means here object of account class is passed with a debit card in the back end so that whatever amount withdrawn by you get deducted from your account and reflected a new amount to your account balance.

Example of a call by value using java program

[c]
public class Main {
public static void main(String[] args) {
int a = 5;
System.out.println(“before calling modifyValue method Value of a – ” +a);
modifyValue(a);
System.out.println(“after calling modifyValue method Value of a – ” +a);
}

public static void modifyValue(int x) {
x = 10;
System.out.println(“Value of x inside method – “+x);
}
}
[/c]

Output:

Let’s see an example of a call by reference using java program

[c]
class ModifyValue
{
int a,b;
public void modifyValue(ModifyValue obj){
obj.a += 20;
obj.b +=20;
}
}

class Main{
public static void main(String args[]){
ModifyValue obj= new ModifyValue();
obj.a=10;
obj.b=20;
System.out.println(“Values of a and b before method call =>”);
System.out.println(obj.a);
System.out.println(obj.b);
obj.modifyValue(obj);
System.out.println(“Values of a and b after method call =>”);
System.out.println(obj.a);
System.out.println(obj.b);
}
}
[/c]

Output:

I hope now you have cleared the concept of call by value and call by reference in java. If still have some doubt then please do comment.