Call by Value and Call by Reference in Java

There is only call by value in java, not call by reference. If we call a method passing a value, it is known as call by value. The changes being done in the called method, is not affected in the calling method.

In Java, when you pass a primitive type to a method, it is passed by value. Thus, what occurs to the parameter that receives the argument has no effect outside the method. For example, In the following program, the operations that occur inside changeData( ) have no effect on the values da1 and da2 used in the call; their values here did not change to 40 and 40.

Example
class Data { void changeData(int da1,int da2) { da1+=20; da2+=20; } } public class Javaapp { public static void main(String[] args){ int data1 = 20,data2 = 20; System.out.println("data1 = "+data1); System.out.println("data2 = "+data2); Data dataobj = new Data(); dataobj.changeData(data1, data2); System.out.println("After dataobj.changeData()"); System.out.println("data1 = "+data1); System.out.println("data2 = "+data2); } }


Output:
data1=20 data2=20 After dataobj.changeData() data1=20 data2=20
Example of call by value in java
class Operation{ int data=90; void change(int data){ data=data+100;//changes will be in the local variable only } public static void main(String args[]){ Operation op=new Operation(); System.out.println("before change "+op.data); op.change(500); System.out.println("after change "+op.data); } }


Output:
before change 90 after change 90
Another Example of call by value in java

In case of call by reference original value is changed if we made changes in the called method. If we pass object in place of any primitive value, original value will be changed. In this example we are passing object as a value. Let's take a simple example:

class Operation2{ int data=50; void change(Operation2 op){ op.data=op.data+100;//changes will be in the instance variable } public static void main(String args[]){ Operation2 op=new Operation2(); System.out.println("before change "+op.data); op.change(op);//passing object System.out.println("after change "+op.data); } }


Output:
before change 50 after change 150
Call by Reference

When you pass an object to a method, the situation changes dramatically, because objects are passed by what is effectively call-by-reference. Keep in mind that when you create a variable of a class type, you are only creating a reference to an object. Thus, when you pass this reference to a method, the parameter that receives it will refer to the same object as that referred to by the argument. This effectively means that objects are passed to methods by use of call-by-reference. Changes to the object inside the method do affect the object used as an argument. For example, In the following program, the actions inside changeData( ) have affected the object used as an argument.

class Data { int data1 = 20; int data2 = 20; void changeData(Data obj,int da1,int da2) { obj.data1+=da1; obj.data2+=da2; } } public class Javaapp { public static void main(String[] args){ Data dataobj = new Data(); System.out.println("dataobj.data1 = "+dataobj.data1); System.out.println("dataobj.data2 = "+dataobj.data2); dataobj.changeData(dataobj, 20, 20); System.out.println("After dataobj.changeData()"); System.out.println("dataobj.data1 = "+dataobj.data1); System.out.println("dataobj.data2 = "+dataobj.data2); } }


Output:
dataobj.data1=20 dataobj.data2=20 After dataobj.changeData() dataobj.data1=40 dataobj.data2=40



Instagram