IT Community - Software Programming, Web Development and Technical Support

What is the main difference between pass-by-reference and pass-by-value?

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?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > Java Programming

Register FAQ Members List Calendar Mark Forums Read
  #1  
Old 07-26-2007, 03:34 AM
oxygen oxygen is offline
D-Web Architect
 
Join Date: Jun 2007
Posts: 632
oxygen is on a distinguished road
Question What is the main difference between pass-by-reference and pass-by-value?

What is the main difference between pass-by-reference and pass-by-value?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 07-26-2007, 03:54 AM
kingmaker kingmaker is offline
D-Web Genius
 
Join Date: Jun 2007
Posts: 881
kingmaker is on a distinguished road
Send a message via MSN to kingmaker
Default Re: What is the main difference between pass-by-reference and pass-by-value?

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 08-07-2007, 12:15 AM
leoraja8 leoraja8 is offline
D-Web Sr.Programmer
 
Join Date: May 2007
Posts: 190
leoraja8 is on a distinguished road
Default Re: What is the main difference between pass-by-reference and pass-by-value?

pass-by-reference returns a address of the value.
pass-by-value returns a value.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

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


All times are GMT -7. The time now is 03:10 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0