| |
GeekInterview.com > Interview Questions > J2EE > OOPS
| Print | |
Question: what is the difference between call by value, call by reference, call by address?
Answer: no answers |
| August 08, 2008 11:54:02 |
#5 |
| abhatnagar192006 |
Member Since: August 2008 Total Comments: 1 |
RE: what is the difference between call by value, call by reference, call by address? |
JAVA does not support call by reference. As far as call by value is concerned..it means that java primitives are passed as arguments to a method.
Eg: void swapNumbers (int a, int b) ;
When we talk of passing by reference...we are actually passing "object references" by value.
Eg:
Class Student { ................. }
Class containsMain { public static void main (String args[]) { Student rajeev = new Student ("rajeev") ; Student mohan = new Student("mohan") ; void swapObjects ( rajeev, mohan) ; ............................. } }
In the implementation of swapObjects, we have the folowing...
swapObjects (Student x, Student y) {
Student temp = new Student (" ");
temp = x; x = y; y = temp;
}
Before, This function is invoked, x refers to rajeev and y refers to mohan. During the function call, the reference of rajeev is copied to x and that of mohan is copied to y.
In the method, the object references are swapped. However, when the method returns, x and y are abandoned, while the original references of the objects rajeev and mohan are still the same.
If JAVA supported call by reference, the swap would be successful..but it was not.
Summary: JAVA does not support call by reference, but only passing on references of objects by value
Aayush
|
| |
Back To Question | |