What is the difference between call by value, call by reference, call by address?

No answers

Questions by bhatiagagan5

Showing Answers 1 - 24 of 24 Answers

jamanat

  • Dec 26th, 2007
 

I guess call by value and call by reference everyone knows.
In Java:
Call By value - premitive types passed in the method call, push their values on stack and hence said as called by Values.

Call By Reference - All objects passed to any method call, pass their reference on stack and hence said as called by reference.

Remiaining is call by address (should be same as call by reference), if not, the person may be addressing following:
     Static final member variables (constants) passed in the method call which are in the permanent memory and only the address (or reference) is pushed on stack. As the value on this address can not be changed (only read), the person is refering it as address and not reference.

Hope this helps. Even if you dont answer the person exactly, if you give above answer, interview pannel will understand that your concepts are clear and that's what you need to show.

Thanks,
Jamanat

sachidanand

  • Apr 15th, 2008
 

Call by value: A copy of the argument is passed as parameter. So the caller method and the called method are working on different sets of data. Changing one doesn't affect the other

Call by reference:The address of the object on the heap, w.r.t java, is passed as the parameter. So modifying one will have an effect on the other.

Call by address: same as call by reference.

Hope your question is answered, if not already answered.

  Was this answer useful?  Yes

abhatnagar192006

  • Aug 4th, 2008
 

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


  Was this answer useful?  Yes

mani_miit

  • Dec 14th, 2009
 

call by value--->You are passing primitive value for your method argument.
call by ref--->You are passing object
call by address-->reference and address both are same.

  Was this answer useful?  Yes

Milan unjiya

  • Jan 2nd, 2014
 

Call by value---> when we pass normal variable in method, it is call by value.

Call by reference---> when we pass object as parameter, it is passed by reference.

Call by address---> call by reference and call by address both are same.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions