File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between call by value and call by ref. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between call by value and call by ref." Watch "Difference between call by value and call by ref." New topic
Author

Difference between call by value and call by ref.

kittu anu
Greenhorn

Joined: Jan 18, 2002
Posts: 28
Hi all,
G3 is my screen name!
I always get confused with call by ref and call by value.Can some help me in recognizing the diff between call-by-value and call-by-ref.
Appreciate ur help.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Originally posted by G3:
[QB]Hi all,
G3 is my screen name!

That's gonna change soon, don't worry

I always get confused with call by ref and call by value.Can some help me in recognizing the diff between call-by-value and call-by-ref.

Well, ther is a good "tutorial" here:
http://www.javaranch.com/campfire/StoryPassBy.jsp
Come back if the tutorial doesn't answer your question
HIH


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
lol
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Ok, here is a simple rule to remember that will really help make this clear:
Java always passes parameters by value.
More specifically,
Java passes all method arguments by passing a copy of the argument value.
There are two types of variables, primitive variables and reference variables
A primitive variable holds a primitive value. Some examples:
int i = 5;
byte b = 12;
boolean bool = true;
float f = 3.0f;
etc.
The other kind of variables are reference variables. They hold references to objects.
String myString = "Hello";
myString is a reference variable. It contains a reference to the object on the right side of the expression.
here's another one:
Object myObjectReference = new Object();
Again, here we have an object being created on the right, and a reference to this new object is being stored in the reference variable on the left.
myObjectReference references the new object; it is not the object itself.
For example, here's a second variable:
Object mySecondObjectReference = myObjectReference;
This second reference variable references the same object as the first one; we have only created ONE object.
A final example...
int[] myArrayReference = new int[10];
Here, on the right, we are actually creating a new object of type 'int array'. A reference to the object is being stored in the variable on the left.
Now...as for passing these variables to methods.
When you pass a variable to a method, what you are passing is merely a copy of the value of that variable.


The println() above always prints hi12. The reason that the foo method didn't change the values of the string or the int is that you only passed copies of their values to the method. You passed a copy of the value of variable anInt, which was 12. And you passed a copy of the value of variable aString, which is a reference to a string object.
In the method, these values you have passed are assigned to local parameter variables myString and myInt. Then it changes the object refereneced by myString, and changed the value of myInt. But since you only passed copies of the values to the method, the original values before the method call have not changed.
Some programming languages have pass-by-reference...but just remember that java does not. It only has pass by value. And if you remember you are actually passing a copy of the value, it should make it very clear what is happening when you call a method with parameters.
Good luck.
Rob
[ January 24, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Excellent job Rob
Originally posted by Paul Salerno:
lol

What's so funny, buddy ?
[ January 24, 2002: Message edited by: Valentin Crettaz ]
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Thanks Valentin!

Originally posted by Valentin Crettaz

What's so funny, buddy ?


I think he was chuckling at this line:

Originally posted by G3:
Hi all,
G3 is my screen name!

That's gonna change soon, don't worry


Rob
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610

I think he was chuckling at this line:
------------------------------------------
Originally posted by G3:
Hi all,
G3 is my screen name!
-----------------------------------------

RIGHT
kittu anu
Greenhorn

Joined: Jan 18, 2002
Posts: 28
Thanks Rob,
To all others thanks for making fun of my screen name!but I changed my name.Any how sorry i didn't see the rules for screen name
[ January 24, 2002: Message edited by: kittu anu ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
We were not making fun of your screen name, I was just trying to tell you that you'd have to change it sooner or later...
Anyway, bottom line is you changed it and we thank you for that.
Now, no more screen-name-smiling-stuff, let's get back to work...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between call by value and call by ref.