Java Pass By Reference And Pass By Value With Examples

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

Learn about Java Pass by Reference & Pass by Value and how it works via practical examples demonstrating the parameter passing techniques:

This tutorial will explain Java ‘pass by reference’ which is a parameter passing technique used in Java. Here we will be exploring the technique in detail along with syntax and programs that illustrate the use of passing the parameter by reference.

We will also ensure that each and every aspect of this method is covered as a part of this tutorial so that you will have a better understanding of the topic.

=> Take A Look At The Java Beginners Guide Here

Java Pass By Reference And Pass By Value

Java Pass by Reference

There are basically two types of techniques for passing the parameters in Java. The first one is pass-by-value and the second one is pass-by-reference. One thing to remember here is that when a primitive type is passed to a method, then it is done by the use of pass-by-value.

However, all the non-primitive types that include objects of any class are always implicitly passed by use of pass-by-reference.

Basically, pass-by-value means that the actual value of the variable is passed and pass-by-reference means the memory location is passed where the value of the variable is stored.

Java Pass By Value Example

In this example, we will showcase how to pass a parameter by using pass-by-value which is also known as call-by-value.

Here we have initialized a variable ‘a’ with some value and used the pass-by-value technique to show how the value of the variable remains unchanged. In the next segment, we will try to show a similar example, but we will use non-primitives.

public class Example {
	
	/*
	 *  The original value of a will remain unchanged in
	 *  case of call-by-value
	 */
	
	int a = 10;
	void call(int a) {
		
		// this local variable a is subject to change in its value
		a = a+10;
	}
	
	public static void main(String[] args) {

		Example eg = new Example();
		System.out.println("Before call-by-value: " + eg.a);
		
		/*
		 * Passing an integer 50510 to the call() method. The value of 
		 * 'a' will still be unchanged since the passing parameter is a 
		 * primitive type. 
		 */
		eg.call(50510);
		System.out.println("After call-by-value: " + eg.a);
		
		
	}
}

Output:

Java pass by value

Java Passing Object: Pass by Reference Example

In this example, we will see how to pass any object of a class using pass-by-reference.

As you can see, when we have passed the object reference as a value instead of a value, the original value of the variable ‘a’ is changed to 20. This is because of the changes in the called method.

public class Example {
	
	/*
	 *  The original value of 'a' will be changed as we are trying
	 *  to pass the objects. Objects are passed by reference.
	 */
	
	int a = 10;
	void call(Example eg) {
		eg.a = eg.a+10;
	}
	
	public static void main(String[] args) {

		Example eg = new Example();
		System.out.println("Before call-by-reference: " + eg.a);
		
		// passing the object as a value using pass-by-reference
		eg.call(eg);
		System.out.println("After call-by-reference: " + eg.a);
		
		
	}
}

Output:

Java passing Object - pass by Reference

Ways To Create A Pass-by-Reference

Java supports pass-by-value,0 but there are three different ways to create a pass-by-reference in Java.

  • Make the member variable public inside a class.
  • Return a value from a method and update the same inside the class.
  • Create a single element array and pass it as a parameter to the method.

Making The Member Variable Public

In this technique, the object of a class is passed to the add() method and it updates the public member variable ‘a’. You can see that the original memory address where the value is stored has been changed.

public class Example {
	
	// making a public member variable
	public int a;
	
	public Example() {
		a = 10;
	}
	
	public static void main(String[] args) {

		Example eg = new Example();
		
		// Before calling the add() method
		System.out.println("Before calling method: " +eg.a);
		
		// calling method
		add(eg);
		
		// After calling the add() method
		System.out.println("after calling method: " +eg.a);
		
	}
	
	// add() method starts here that increments 'a' by 1
	public static void add(Example obj) {
		obj.a++;
	}
	
}

Output:

Making the member variable Public

Returning A Value From A Method

In this technique, we are trying to return a value from add() method as we have changed the type from “void” to “int”. The changes or addition in the value is returned by the add() method and the original memory address has been updated.

public class Example {
	
	public static void main(String[] args) {
		
		int a = 10;
		
		// Before calling the add() method
		System.out.println("Before calling method: " +a);
		
		// calling method
		a = add(a);
		
		// After calling the add() method
		System.out.println("after calling method: " +a);
		
	}
	
	// add() method starts here that increments 'a' by 1
	public static int add(int a) {
		a++;
		return a;
	}
	
}

Output:

Returning a value from a method

Creating A Single Element Array & Passing As A Parameter

In this technique, we have created a single element array and passed it as a parameter to the method add(int a[]). You can see that the original memory address is changed in this case as well.

public class Example {
	
	public static void main(String[] args) {
		
		// single element array
		int a[] = {10};
		
		// Before calling the add() method
		System.out.println("Before calling method: " +a[0]);
		
		// calling method
		add(a);
		
		// After calling the add() method
		System.out.println("after calling method: " +a[0]);
		
	}
	
	// add() method starts here that increments 'a' by 1
	public static void add(int a[]) {
		a[0]++;
	}
	
}

Output:

Creating a single element array

Frequently Asked Questions

Q #1) Can you pass by reference in Java?

Answer: Java supports pass by value and we can not pass primitive types to a method directly by using pass by reference. However, there are different ways to create a pass by reference as discussed above.

Q #2) Does Java pass arrays by reference?

Answer: Java supports pass by value but when we are dealing with objects such as Java array objects, then the object reference is passed to the method.

Q #3) Does Java pass objects by reference or value?

Answer: This won’t be wrong to say that the “Objects in Java are passed by reference”. But if you want a technically correct statement then the above statement can also be put as “Object references in Java are passed by value”.

Q #4) Explain why there is no call by reference in Java.

Answer: Call by reference needs the memory location to be passed and these memory locations further require pointers which Java does not have. Hence, there is no call by reference in Java.

Q #5) Why pointers are not used in Java?

Answer: Unlike the C language, Java does not have pointers. The prime reason for not using pointers in Java can be security as pointers may compromise the security that comes along with Java. The use of Pointers might have made Java more complex.

Conclusion

In this tutorial, we have explained pass-by-value and pass-by-reference by highlighting the difference between the two. Also, we have explained pass-by-reference with some of the common examples with the help of object passing.

We have also explained the different techniques using which we can create a pass-by-reference and each of these techniques was explained properly with an example to help you understand in detail.

=> Read Through The Easy Java Training Series

Was this helpful?

Thanks for your feedback!

Leave a Comment