Does Java is pass by value or pass by reference is one of the tricky
Java question mostly asked on fresher level interviews. Before debating
whether Java is pass by value or pass by reference lets first clear what is
pass by value and what is pass by reference. This question has its origin on C
and C++ where you can pass function parameter either value or memory address,
where value is stored (pointer). As per Java specification everything in Java
is pass by value whether its primitive value or objects and it does make sense
because Java doesn't support pointers or pointer arithmetic, Similarly multiple inheritance and operator
overloading is also not supported in Java. This question becomes confusing
when interviewer ask about how object is passed in Java ? Answer to this
question is simple whenever a method parameter expect object, reference of that
object is passed. Many programmer confuses reference with pointers here which
is not correct, reference is a kind of handle which is used to locate object or
change object, but it doesn’t allows any pointer arithmetic i.e. you can not
increase or decrease memory address and locate a different object
using reference in Java.
Pass by Value and Pass by Reference Example in Java
Let’s see two example of calling method and passing parameter this will
clear any doubt whether Java is pass by value or pass by reference. consider
following example:
public class
PassByValueExample {
public static void main(String args[]) {
int number = 3;
printNext(number);
System.out.println("number Inside main(): "+number);
}
public static void printNext(int number){
number++;
System.out.println("number Inside printNext(): "+number);
}
}
Output:
number Inside printNext(): 4
number Inside main(): 3
public static void main(String args[]) {
int number = 3;
printNext(number);
System.out.println("number Inside main(): "+number);
}
public static void printNext(int number){
number++;
System.out.println("number Inside printNext(): "+number);
}
}
Output:
number Inside printNext(): 4
number Inside main(): 3
Above example clearly shows that primitives are passed as pass by value
to method parameters, had Java pass by reference both main
method and printNext() would have
printed same value. Now look at another example of passing object as method
parameter which will confuse you that Java is pass by reference, which Java is
not.
public class
PassByReferenceConfusion {
public static void main(String args[]) {
Car car = new Car("BMW");
System.out.println("Brand of Car Inside main() before: "+ car.brand);
printBrand(car);
System.out.println("Brand of Car Inside main()after: "+ car.brand);
}
public static void printBrand(Car car){
car.brand = "Maruti";
System.out.println("Brand of Car Inside printBrand(): "+car.brand);
}
private static class Car{
private String brand;
public Car(String brand){
this.brand = brand;
}
}
}
Output:
Brand of Car Inside main() before: BMW
Brand of Car Inside printBrand(): Maruti
Brand of Car Inside main()after: Maruti
public static void main(String args[]) {
Car car = new Car("BMW");
System.out.println("Brand of Car Inside main() before: "+ car.brand);
printBrand(car);
System.out.println("Brand of Car Inside main()after: "+ car.brand);
}
public static void printBrand(Car car){
car.brand = "Maruti";
System.out.println("Brand of Car Inside printBrand(): "+car.brand);
}
private static class Car{
private String brand;
public Car(String brand){
this.brand = brand;
}
}
}
Output:
Brand of Car Inside main() before: BMW
Brand of Car Inside printBrand(): Maruti
Brand of Car Inside main()after: Maruti
If you see change made in method parameter is reflected globally
i.e. brand of car is changed in all places it means one object is used in both
method. Well in reality if you pass object as method parameter in Java it passes "value of reference" or in
simple term object reference or handle to Object
in Java. Here reference term is entirely different than
reference term used in C and C+ which directly points to memory address of
variable and subject to pointer arithmetic. in Java object can only be
accessed by its reference as you can not get memory address where object is
stored or more precisely there is no method to get value of object by passing memory
address.
To conclude everything in Java including primitive and objects are pass
by value. In case of object value of reference is passed.
Other Java Interview articles you may like
3 comments:
A slightly modified version (picked up from stack over flow). Assigning a new object in called method may make it clear how it would not affect object in calling method.
public static void main(String args[]) {
Car car1 = new Car("BMW");
System.out.println("Brand of Car Inside main() before: " + car1.brand);
printBrand(car1);
System.out.println("Brand of Car Inside main() after printBrand: " + car1.brand);
printBrandAgaian(car1);
System.out.println("Brand of Car Inside main() after printBrandAgain: " + car1.brand);
}
public static void printBrand(Car car2) {
car2.brand = "Maruti";
System.out.println("Brand of Car Inside printBrand(): " + car2.brand);
}
public static void printBrandAgaian(Car car3) {
car3 = new Car("Mercdes");
System.out.println("Brand of Car Inside printBrandAgain(): " + car3.brand);
}
I knew that Java does not support pass by reference simply because lack of pointers in Java but object reference always seems confusing to me because it looks exactly similar to pass by reference. Thanks for clear wording which helps to isolate this single case in Java. By the this is also referred as call by value and call by reference :)
Whether or not there is "value copy" depends on the compiler. And yes, Java holds all "copies" of a reference as actual parameters when it is passed to methods.
The real "pass-by-reference" then should replace formal parameters by what is passed in to let everything in the "passing chain" share the same reference.
Post a Comment