Pass by Reference - Not supported by Java
In some programming languages, like C++, there is a concept called pass by reference. When a variable is passed by reference, the method receives a direct reference to the original variable, not just a copy of its value. This means that any changes made to the variable inside the method will affect the original variable outside the method.
However, Java does not support pass by reference for variables. In Java, only pass by value is used. This means that when an object reference is passed to a method, the method receives a copy of the reference, not a direct reference to the original object.
It's important to note that while Java doesn't support pass by reference for variables, it does allow you to achieve a similar effect when working with objects.
How Java Handles Pass by Reference Using Pass by Value:
Although Java doesn't support pass by reference directly, it does allow you to achieve a similar effect when working with objects. This is because when an object reference is passed to a method, the method receives a copy of the reference that points to the same object in memory.
For example :
Java
public class PassByReferenceExample {
public static void main(String[] args) {
Person person = new Person("Rahul", 25);
System.out.println("Before method call: " + person.getName()); // Output: Rahul
changePersonName(person);
System.out.println("After method call: " + person.getName()); // Output: Harsh
}
public static void changePersonName(Person p) {
p.setName("Harsh");
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Before method call: Rahul
After method call: Harsh
In this example, we have a `Person` class with a `name` & `age` property. We create an instance of `Person` called `person` with the name "Rahul" & age 25. We pass the `person` object to the `changePersonName` method, which changes the name property to "Harsh".
When we print the name of the `person` object after the method call, we see that it has indeed changed to "Harsh". This is because the `changePersonName` method received a copy of the reference to the `person` object, & any changes made to the object inside the method affected the original object.
So, while Java doesn't support pass by reference directly, it allows you to modify the state of an object passed to a method, giving you a similar effect.
Pass by Value Using Java Primitive Types
When working with primitive types in Java, such as `int`, `double`, `boolean`, etc., pass by value is straightforward. The value of the primitive variable is copied & passed to the method, & any changes made inside the method do not affect the original variable.
For example :
Java
public class PassByValuePrimitiveExample {
public static void main(String[] args) {
int num = 10;
System.out.println("Before method call: " + num); // Output: 10
changeValue(num);
System.out.println("After method call: " + num); // Output: 10
}
public static void changeValue(int x) {
x = 20;
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Before method call: 10
After method call: 10
In this example, we have a primitive variable `num` with an initial value of 10. We pass `num` to the `changeValue` method, which attempts to change its value to 20. However, when we print the value of `num` after the method call, we see that it remains unchanged at 10. This is because a copy of the value was passed to the method, & any changes made inside the method affected only the copy, not the original variable.
Primitive types in Java are always passed by value, & there's no way to directly change the value of a primitive variable passed to a method from within that method.
Pass by Value Using Java Objects
When passing objects in Java, the behavior is slightly different compared to primitive types, but it still follows the pass by value principle. When an object reference is passed to a method, a copy of the reference is created & passed, not the actual object itself.
For example :
Java
public class PassByValueObjectExample {
public static void main(String[] args) {
Person person = new Person("Rinki", 30);
System.out.println("Before method call: " + person.getName()); // Output: Rinki
changePersonName(person);
System.out.println("After method call: " + person.getName()); // Output: Sanjana
}
public static void changePersonName(Person p) {
p.setName("Sanjana");
p = new Person("Ravi", 35);
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

You can also try this code with Online Java Compiler
Run Code
Output
Before method call: Rinki
After method call: Sanjana
In this example, we create a `Person` object called `person` with the name "Rinki" & age 30. We pass the `person` object to the `changePersonName` method. Inside the method, we change the name of the person to "Sanjana" using the `setName` method, & then we create a new `Person` object with the name "Ravi" & age 35 & assign it to the `p` reference.
When we print the name of the `person` object after the method call, we see that it has changed to "Sanjana". However, the `person` object still refers to the original object created in the `main` method, not the new object created inside the `changePersonName` method.
This shows that when passing objects in Java, a copy of the reference is passed, which helps you to modify the state of the original object, but you cannot replace the original object entirely from within the method.
Pass by Value for non-primitives
In Java, when a non-primitive data type, such as an object or an array, is passed to a method, the reference (memory address) of the object is passed by value. This means that a copy of the reference is created & passed to the method, but it still points to the same object in memory.
For example :
Java
public class PassByValueNonPrimitiveExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
System.out.println("Before method call: " + Arrays.toString(numbers));
modifyArray(numbers);
System.out.println("After method call: " + Arrays.toString(numbers));
}
public static void modifyArray(int[] arr) {
arr[0] = 10;
}
}

You can also try this code with Online Java Compiler
Run Code
Output
[1, 2, 3, 4, 5]
[10, 2, 3, 4, 5]
In this example, we have an integer array `numbers` with initial values `[1, 2, 3, 4, 5]`. We pass the `numbers` array to the `modifyArray` method, which modifies the first element of the array to 10.
When we print the contents of the `numbers` array after the method call, we see that the first element has indeed changed to 10. This is because the `modifyArray` method received a copy of the reference to the `numbers` array, & any changes made to the array inside the method affected the original array.
However, if we were to reassign the `arr` reference inside the `modifyArray` method to a new array object, it would not affect the original `numbers` array in the `main` method, as the reference passed is a copy.
This behavior applies to all non-primitive data types in Java, that are : objects, arrays, & collections.
Frequently Asked Questions
Can we achieve pass by reference in Java using the final keyword?
No, using the final keyword does not change the pass by value behavior in Java. It only prevents reassignment of the reference inside the method.
Is it possible to modify the state of an object passed to a method in Java?
Yes, when an object is passed to a method, a copy of the reference is passed, allowing you to modify the state of the object inside the method.
Can we change the value of a primitive variable passed to a method in Java?
No, when a primitive variable is passed to a method in Java, a copy of the value is passed, & any changes made inside the method do not affect the original variable.
Conclusion
In this article, we learned about pass by value & pass by reference concepts in Java. We saw that Java only supports pass by value for both primitive types & objects. However, when objects are passed to methods, a copy of the reference is passed, which allow us to modify the state of the original object
You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.