Pass By Value or Pass By Reference

I have seen many new comers are facing problem to understand the exact flow of these two topics. Both of them has specific meanings which are came from different programming languages. Before going in to details let’s first define what is reference in Java.

Reference is nothing but the memory address of the Object where it resides

Let’s take an example to understand reference properly. Say you have a class Shop and so you can create different kind of Shops with different attributes but all these shops will have unique address where the Shop is located which is the reference of the Shop Object.

What is the difference between Pass By Value and Pass By Reference ?

Argument which is passed in method call for…

  • Pass By Reference : Method will get the same element
  • Pass By Value         : Method will get the copy of element

PByValORPBRef

As you easily understand with above image that when you pass mango to pass by reference it can change colour of your actual mango while pass by value can not.

First of all strictly note that Java is completely Pass By Value

Let’s check following example to understand more.

class StudentResult {
	
	private int percentages;
	
	public StudentResult(int percentages) {
		this.percentages = percentages;
	}

	public int getPercentages() {
		return percentages;
	}

	public void setPercentages(int percentages) {
		this.percentages = percentages;
	}
	
}


public class JavaPassByValue {

	public static void main(String args[]) {
		StudentResult studentResult = new StudentResult(78);
		increaseFivePercent(studentResult);
		System.out.println("Percentages of Student : "+studentResult.getPercentages());
		
		createNewResultOfStudent(studentResult);
		System.out.println("New Percentages of Student : "+studentResult.getPercentages());
	}

	private static void increaseFivePercent(StudentResult studentResult) {
		int current = studentResult.getPercentages();
		studentResult.setPercentages(current + 5);
	}
	
	private static void createNewResultOfStudent(StudentResult studentResult) {
		StudentResult studentResultNew = new StudentResult(52);//New Result of Student 
		studentResult = studentResultNew;
	}
}

In above example, We have created StudentResult Object which is passed as an argument to the method increaseFivePercent and createNewResultOfStudent and we will have following output.

Percentages of Student : 83
New Percentages of Student : 83

In first case we have passed StudentResult Object in which we are passing value of reference to the method. So, indirectly now method will have reference or location of actual StudentResult Object so ultimately when we call studentResult.setPercentages(current + 5); it will reflect the result in actual StudentResult of Main method.

But in second case when we call createNewResultOfStudent method which is trying to allocate new reference address to the passed StudentResult, it will not going to change the actual memory address of StudentResult of Main method.

Note that studentResult of createNewResultOfStudent and StudentResult of Main method both are referencing to the same Object or memory location but both have different identity so that change in reference of one will not be reflected to other.

Now as it is quite easy to understand the same thing about primitive types in java. Let’s take a look at following method.

private static void square(int number) {
     number = number * number;
}

When we call this method it will not change our actual number and we know that why and so, from above examples we can conclude that Java is totally Passed By Value.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s