Pass by value in programming means passing a copy of the argument to a function or a method, and it does not change the original value. Pass by value in Java refers to the way in which primitive data types and object references are passed as an argument to the methods.
In Java, all data types primitive (e.g., int, char, boolean) and wrapper (object reference) types are passed by value. This means that when a primitive data type or object reference is passed as an argument to a method, a copy of the value is made and passed to the method. It does not send the original value itself.
Primitive type in Java is pass by value
Primitive types in Java are passed by value. To verify whether Java is a pass by value or not let’s see an example below:
public class PassByValueExample {
public static void main(String[] args) {
int firstNumber = 5;
int secondNumber = 10;
swap(firstNumber, secondNumber);
System.out.println("The value of firstNumber is: " + firstNumber);
System.out.println("The value of secondNumber is: " + secondNumber);
}
private static void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
}
The output of the above code is given below:
The value of firstNumber is: 5
The value of secondNumber is: 10
The swap()
method in the example above takes two integer arguments, x
and y
, and attempts to swap their values. This basically means changing the value of a variable firstNumber
to 10 and the value of secondNumber
to 5. However, when the method is called and the values of firstNumber
and secondNumber
are passed as arguments, and only copies of these values are passed to the method. This means that the original values of firstNumber
and secondNumber
are not modified. The variables remain unchanged after the method is called.
Object reference type in Java is pass by value
It is important to note that while object references are also passed by value in Java, the objects themselves are not. This means that if an object reference is passed as an argument to a method and the object is modified within the method, the changes will be reflected in the original object. For example:
public class ObjectPassByValueExample {
public static void main(String[] args) {
Student student = new Student();
student.setRollNo(5);
System.out.println("Student roll number before modifying is " + student.getRollNo());
modifyObject(student);
System.out.println("Student roll number after modifying is " + student.getRollNo());
}
private static void modifyObject(Student obj) {
obj.setRollNo(10);
}
}
class Student {
private int rollNo;
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
}
The output of the above code is:
Student roll number before modifying is 5
Student roll number after modifying is 10
The modifyObject()
method in the above example code takes a Student
object as an argument and modifies its value. When the method is called and the value of obj
is passed as an argument, a copy of the reference to the Student
is made and passed to the method. Since the Student
itself is not passed by value, the changes made to the object within the method are reflected in the original object.
Conclusion
Java uses a pass by value approach for both primitive data types and object references. While this may seem counterintuitive at first, it is important to understand this distinction in order to write correct and efficient code in Java.
Leave a Reply