Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between call by value and call by ref.

 
kittu anu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lol
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

RIGHT
 
kittu anu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic