This is a discussion on What is the main difference between pass-by-reference and pass-by-value? within the Java Programming forums, part of the Software Development category; What is the main difference between pass-by-reference and pass-by-value?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
|
#1
| |||
| |||
| What is the main difference between pass-by-reference and pass-by-value? |
|
#2
| |||
| |||
| Other languages use pass-by-reference or pass-by-pointer. But in Java no matter what type of argument you pass the corresponding parameter (primitive variable or object reference) will get a copy of that data, which is exactly how pass-by-value (i.e. copy-by-value) works. In Java, if a calling method passes a reference of an object as an argument to the called method then the passedin reference gets copied first and then passed to the called method. Both the original reference that was passed-in and the copied reference will be pointing to the same object. So no matter which reference you use, you will be always modifying the same original object, which is how the pass-by-reference works as well. ref d Pass-by-value for primitive variables vs Object references public void first(){ int i= 10; int x = second(i); //At this point //value of i is still 10 //value of x is 11 } public int second(int k) { k++; return k ; } i = 10 k = 10 k = 11 Copy of I stores i copies I acts on k ref public void first(){ Car c = new Car("red") //At this point //color is Red second(c); //At this point //color is Blue } public void second(Car d) { d.setColor(blue); //color is blue } Car object String color = red ref c copy of c Primitive variables Object references modifies the original object through copied reference modifies the copy k but not the original. Changes color = blue If your method call involves inter-process (e.g. between two JVMs) communication, then the reference of the calling method has a different address space to the called method sitting in a separate process (i.e. separate Java – Fundamentals JVM). Hence inter-process communication involves calling method passing objects as arguments to called method by-value in a serialized form, which can adversely affect performance due to marshaling and unmarshaling cost. |
|
#3
| |||
| |||
| pass-by-reference returns a address of the value. pass-by-value returns a value. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how to pass variables from one page to another? | senraj | PHP Programming | 6 | 01-29-2008 02:56 AM |
| 1) Record 2) Pass up 3) As Object 4) Ignore. | vigneshgets | Testing Tools | 1 | 01-18-2008 01:11 AM |
| Is it possible to Pass Query String to Javascript? | velhari | HTML, CSS and Javascript Coding Techniques | 1 | 11-20-2007 09:47 PM |
| How to pass array of string from vc++ dll to C# ? | kingmaker | C and C++ Programming | 2 | 08-10-2007 04:35 AM |
| How to Pass a string value from vc++ .net dll to c#? | kingmaker | C and C++ Programming | 1 | 07-24-2007 04:37 AM |
Our Partners |