One of the biggest confusion in Java programming language is whether java is Pass by Value or Pass by Reference.

First of all we should understand what is meant by pass by value or pass by reference.

  • Pass by Value: The method parameter values are copied to another variable and then the copied object is passed, that’s why it’s called pass by value.
  • Pass by Reference: An alias or reference to the actual parameter is passed to the method, that’s why it’s called pass by reference.

Now let’s see with this example why Java is Pass by Value

public class Bike {
	private String color;
	public Bike(){}
	public Bike(String c){
		this.color=c;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
}

public class PassByValue {

	public static void main(String[] args) {

		Bike red = new Bike("Red"); //memory reference 50
		Bike blue = new Bike("Blue"); //memory reference 100
		
		swap(red, blue);
		System.out.println("red color="+red.getColor());
		System.out.println("blue color="+blue.getColor());
		
		foo(blue);
		System.out.println("blue color="+blue.getColor());
		
	}

	private static void foo(Bike bike) { //bike=100
		bike.setColor("Red"); //bike=100
		bike = new Bike("Green"); //bike=200
		bike.setColor("Blue"); //bike = 200
	}

	//Generic swap method
	public static void swap(Object o1, Object o2){ //o1=50, o2=100
		Object temp = o1; //temp=50, o1=50, o2=100
		o1=o2; //temp=50, o1=100, o2=100
		o2=temp; //temp=50, o1=100, o2=50
	} //method terminated
	
}

Output:

red color=Red
blue color=Blue
blue color=Red

If you look at the first two lines of the output, it’s clear that swap method didn’t worked.

Let’s break above code and see what it actually does.

Bike red = new Bike("Red"); //memory reference 50
Bike blue = new Bike("Blue"); //memory reference 100
  • When we use new operator to create an instance of a class, the instance is created and the variable contains the reference location of the memory where object is saved.
  • For our example, let’s assume that “red” is pointing to 50 and “blue” is pointing to 100 and these are the memory location of both Balloon objects.
//Generic swap method
public static void swap(Object o1, Object o2){ //o1=50, o2=100
	Object temp = o1; //temp=50, o1=50, o2=100
	o1=o2; //temp=50, o1=100, o2=100
	o2=temp; //temp=50, o1=100, o2=50
} //method terminated
  • Now when we are calling swap() method, two new variables o1 and o2 are created pointing to 50 and 100 respectively.
  • When swap(red, blue)is called. 2 new references are created in Stack memory.
  • Notice that we are changing values of o1 and o2 but they are copies of “red” and “blue” reference locations, so actually there is no change in the values of “red” and “blue” and hence the output.
  • Since the variables are just the reference to the objects, we get confused that we are passing the reference so java is pass by reference.
  • However we are passing a copy of the reference and hence it’s pass by value.
  • Now let’s analyze foo() method execution.
private static void foo(Bike bike) { //bike=100
	bike.setColor("Red"); //bike=100
	bike = new Bike("Green"); //bike=200
	bike.setColor("Blue"); //bike = 200
}
  • The first line is the important one, when we call a method the method is called on the Object at the reference location.
  • At this point, balloon is pointing to 100 and hence it’s color is changed to Red.
  • In the next line, ballon reference is changed to 200 and any further methods executed are happening on the object at memory location 200 and not having any effect on the object at memory location 100.
  • This explains the third line of our program output printing blue color=Red.

This explains why Java is Call by Value. Period…

Categories: JAVA

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.