Java Pass-by-Value or Pass-by-Reference

There has been a lot of debate on whether “java is pass by value or pass by reference?“. Well, let us conclude last time, Java is passed by value and not pass-by-reference. If it had been pass-by-reference, we should have been able to C like swapping of objects, but we can’t do that in java. We know it already, right?

1. Java is Pass-by-value

When you pass an object to a method, its memory address is copied bit by bit to a new reference variable, thus both pointing to the same instance. But if you change the reference inside the method, the original reference will not change.

If Java was pass-by-reference, then it would have got changed also.

To prove it, let’s see how memory allocations happen in run time. It should solve the slightest doubts if any. I am using the following program for demonstration of the concept.

public class Foo
{
	private String attribute;

	public Foo (String a){
		this.attribute = a;
	}
	public String getAttribute() {
		return attribute;
	}
	public void setAttribute(String attribute) {
		this.attribute = attribute;
	}
}

public class Main
{
     public static void main(String[] args){
          Foo f = new Foo("f");
          changeReference(f); // It won't change the reference!
          modifyReference(f); // It will change the object that the reference variable "f" refers to!
     }
     public static void changeReference(Foo a) {
          Foo b = new Foo("b");
          a = b;
     }
     public static void modifyReference(Foo c) {
          c.setAttribute("c");
     }
}

2. Analysis

Let us see what happens on runtime step by step when the above program runs:

Foo f = new Foo("f");

This statement will create an instance of class Foo, with ‘attribute’ initialized to ‘f’. The reference to this created instance is assigned to variable f;

1-8158113
public static void changeReference(Foo a)

When this executes then a reference of type Foo with a name a is declared and it’s initially assigned to null.

2-7952427
changeReference(f);

As you call the method changeReference(), the reference a will be assigned to the object which is passed as an argument.

3-7934433
Foo b = new Foo("b");  //inside first method

This will do exactly the same as in the first step, and will create a new instance of Foo, and assign it to b;

4-7475542
a = b;

This is an important point. Here, we have three reference variables, and when the statement executes, a and b will point to the same instance created inside the method. Note: f is unchanged, and it is continually pointing to instance, it was pointing originally. NO CHANGE !!

5-7576774
modifyReference(Foo c);

Now when this statement executed a reference, c is created and assigned to the object with attribute “f”.

6-9650208
c.setAttribute("c");

This will change the attribute of the object that reference c points to it, and the same object that reference f points to it.

8-1270952

I hope that this explanation was clear enough to make your understanding better if it was not already.

Happy Learning !!

Comments

Subscribe
Notify of
guest

6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.