Call by Value and Call by Reference

Call by Value and Call by Reference in Java

Call by Value : In Java, call by value refers to calling a method by passing the value in the parameter in which no changes are reflected in the main method because call by value passes a copy of the variable to the method. When we pass a variable with primitive data types, it is considered a call by values in Java, so any changes to the variable will not be reflected in the caller’s scope.

Program : Call by Value in Java

public class ProgCallByValue {

	public static void increment(int num){
		num = num+1; 
                System.out.println("Value in method: "+num);
    }
    
    public static void main(String[] args) {
        int number=12;
        System.out.println("Value before method call : "+number);
        increment(number);
        System.out.println("Value after method call: "+number);
    }
}
Output :

Value before method call : 12
Value in method: 13
Value after method call: 12

Program : Swapping Of Numbers Using Call by Value in Java

public class ProgCallByValueSwap {

	 public static void main(String[] args){
	      int a = 25;
	      int b = 30;
	      System.out.println("Before swapping, a = " + a + " and b = " + b);
	      swapFunction(a, b);
	      System.out.println("\nBefore and After swapping values will be same :");
	      System.out.println("After swapping, a = " + a + " and b is " + b);
	   }
	 
	  public static void swapFunction(int a, int b) {
	      System.out.println("\nBefore swapping(Inside), a = " + a + " b = " + b);
	      int c = a;
	      a = b;
	      b = c;
	      System.out.println("After swapping(Inside), a = " + a + " b = " + b);
	   }
}
Output :

Before swapping, a = 25 and b = 30

Before swapping(Inside), a = 25 b = 30
After swapping(Inside), a = 30 b = 25

Before and After swapping values will be same :
After swapping, a = 25 and b is 30

Call by Reference : In Java, in call by reference instead of passing the value directly, we pass a reference to the argument variables which means we pass the address of variables/ arguments to functions when making function calls.Here all the changes also reflect in the main method. In call by reference a copy of the reference is created which points to the same address and then is passed as value to the method.

Program : Call by Reference in Java using Object

public class ProgCallByReference {

	int number=10;
    
    public static void increment(ProgCallByReference progcallref){
    	progcallref.number = progcallref.number+1; 
        System.out.println("Value in method: " + progcallref.number);
    }
    
    public static void main(String[] args) {
    	ProgCallByReference obj=new ProgCallByReference();
        
        System.out.println("Value before method call: " + obj.number);
        increment(obj); 
        System.out.println("Value after method call: " + obj.number);
    }
}
Output :

Value before method call: 10
Value in method: 11
Value after method call: 11

Program : Call by Reference in Java using Array

public class ProgCallByRefArray {

	public static void increment(int[] number){
        number[0] = number[0]+1; 
        System.out.println("Value in method: " + number[0]);
    }
    
    
    public static void main(String[] args) {
        int[] number=new int[1]; 
        number[0]=12; 
        System.out.println("Value before method call: " + number[0]);
        increment(number);
        System.out.println("Value after method call: " + number[0]);
        
    }
}
Output :

Value before method call: 12
Value in method: 13
Value after method call: 13

Program : Call by Reference in Java using String

public class ProgCallByRefString {

	public static void change(StringBuilder s){
        s.append(" Softwares"); 
        System.out.println("Value in method: " + s.toString());
    }
    
    
    public static void main(String[] args) {
        StringBuilder s=new StringBuilder("Ksamyatam");
        
        System.out.println("Value before method call: " + s.toString());
        change(s);
        System.out.println("Value after method call: " + s.toString());
        
    }
}
Output :

Value before method call: Ksamyatam
Value in method: Ksamyatam Softwares
Value after method call: Ksamyatam Softwares

Program : Swapping Of Numbers Using Call by Reference in Java

public class ProgCallByRefSwap {
	
	 public static void main(String[] args) {
	      IntWrapper a = new IntWrapper(30);
	      IntWrapper b = new IntWrapper(45);
	      System.out.println("Before swapping, a = " + a.value + " and b = " + b.value);
	      swapFunction(a, b);
	      System.out.println("\nBefore and After swapping values will be different:");
	      System.out.println("After swapping, a = " + a.value + " and b is " + b.value);
	   }
	 
	   public static void swapFunction(IntWrapper a, IntWrapper b) {
	      System.out.println("\nBefore swapping(Inside), a = " + a.value + " b = " + b.value);
	      IntWrapper c = new IntWrapper(a.value);
	      a.value = b.value;
	      b.value = c.value;
	      System.out.println("After swapping(Inside), a = " + a.value + " b = " + b.value);
	   }
}

class IntWrapper {
	   public int value;
	   public IntWrapper(int value)
	   { 
		   this.value = value;
	   }
}
Output :

Before swapping, a = 30 and b = 45

Before swapping(Inside), a = 30 b = 45
After swapping(Inside), a = 45 b = 30

Before and After swapping values will be different:
After swapping, a = 45 and b is 30

Program : Swapping Of Numbers Using Call by Reference in Java (Alternate)

public class IntSwap {

	   public int value;
	   public IntSwap()
	   { 
	   }
	   public IntSwap(int value)
	   { 
		   this.value = value;
	   }
	   
	   public void swap(IntSwap a, IntSwap b) {
		    System.out.println("\nBefore swapping(Inside), a = " + a.value + " b = " + b.value);
		    int temp = a.value;
		    a.value = b.value;
		    b.value = temp;
		    System.out.println("After swapping(Inside), a = " + a.value + " b = " + b.value);
		}
}

public class ProgCallByRefSwap2 {
	
	
	public static void main(String[] args) {
			IntSwap a = new IntSwap(30);
			IntSwap b = new IntSwap(45);
	     
			System.out.println("Before swapping, a = " + a.value + " and b = " + b.value);
	      
			IntSwap obj = new IntSwap();
			obj.swap(a, b);
	      
			System.out.println("\nBefore and After swapping values will be different:");
			System.out.println("After swapping, a = " + a.value + " and b is " + b.value);
	   }
	 
	  
}
Output :

Before swapping, a = 30 and b = 45

Before swapping(Inside), a = 30 b = 45
After swapping(Inside), a = 45 b = 30

Before and After swapping values will be different:
After swapping, a = 45 and b is 30

Note : Java is strictly call by value

Program : Program To Prove Java is call by value

public class ProgJavaIsCallByValue {

	public static void main(String[] args) {
		Result obj1=new Result();
		obj1.number=20;

		Result obj2=new Result();
		obj2.number=30;

        System.out.println("Result1 number before: "+obj1.number);
        System.out.println("Result2 number before: "+obj2.number);

        swap(obj1,obj2);

        System.out.println("\nResult1 number after: "+obj1.number);
        System.out.println("Result2 number after: "+obj2.number);
    }

    public static void swap(Result obj1,Result obj2){
    	Result tmp=obj1;
    	obj1=obj2;
    	obj2=tmp;
    }
}

class Result{
    int number;
}
Output :

Result1 number before: 20
Result2 number before: 30

Result1 number after: 20
Result2 number after: 30

In the above example, we have created two objects of class Result obj1, obj2 and set values of both objects 20 and 30 respectively. Now we will pass both objects in the swap method. In the swap method, we will swap both objects and we will print the values of both objects in the output. We can see that the values of both objects are still the same. It is because when we pass an object in the method java creates a copy of the object and passes it to the method so changes will not reflect in the objects.

Scroll to Top