What is pass-by-value and pass-by-reference?

Ive searched a lot on the difference between these two, but all ive found is stuff written in cryptic jargon.. If someone could please explain exactly what the two are and what the difference is between them (please try to use examples, it helps a lot), id be very grateful.
[281 byte] By [Overkilla] at [2007-11-27 8:18:08]
# 1
Was Wikipedia too cryptic?
Hippolytea at 2007-7-12 20:03:35 > top of Java-index,Java Essentials,Java Programming...
# 2
http://www.javaranch.com/campfire/StoryPassBy.jsp
prometheuzza at 2007-7-12 20:03:35 > top of Java-index,Java Essentials,Java Programming...
# 3

I have a hard time believing google couldn't come up with something you could understand. I wouldn't know how else to explain it. What didn't you understand?

Pass by value is when the parameter's value is copied and that copy is passed to the method. Changing the value of the variable inside the method does not affect the caller.

Pass by reference is when an "alias" to the variable is passed, so it's as if the method is operating directly on the caller's variable. Changing the variable's value inside the method changes the value of the caller's variable.

Java and C only pass by value.

C++ and C# support both PBV and PBR.

http://javadude.com/articles/passbyvalue.htm

http://java.sun.com/developer/JDCTechTips/2001/tt1009.html#tip1

http://www.javaranch.com/campfire/StoryPassBy.jsp

http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

http://www-106.ibm.com/developerworks/library/j-praxis/pr1.html

http://www.cs.toronto.edu/~dianeh/tutorials/params/

http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#38698

http://radio.javaranch.com/channel/val/2004/05/21/1085125887000.html

jverda at 2007-7-12 20:03:35 > top of Java-index,Java Essentials,Java Programming...
# 4
> so does C# I think.Yep.~
yawmarka at 2007-7-12 20:03:35 > top of Java-index,Java Essentials,Java Programming...
# 5

in the next version of java, codenamed fo' jizzle, they're going to introduce a new feature called drive-by reference whereby objects on the heap and references in scope will be randomly matched up to each other by an undisclosed and non-deterministic algorithm, in a big car and baseball cap, known as Pimp Daddy Heap. this is so that any old skool developers who think java is getting a little safe and boring can now convince themselves they're "keepin' it real J-side". the downside, of course, being that for us confused uncles, we'll be forever at the mercy of ClassCastExceptions ( to be renamed "DatShitDontFitException" ) when our references keep pointing to the wrong things, and NullPointerExceptions ( or "FoolDoneGoneLikeElvisExceptions" ) when there aren't enough objects to go round

georgemca at 2007-7-12 20:03:35 > top of Java-index,Java Essentials,Java Programming...
# 6
WOW! Did you make that up yourself?
CaptainMorgan08a at 2007-7-12 20:03:35 > top of Java-index,Java Essentials,Java Programming...
# 7
Pretty fly for a white guy.
Hippolytea at 2007-7-12 20:03:35 > top of Java-index,Java Essentials,Java Programming...
# 8

All of you are totally wrong!!!

Actually, Java has both pass-by-value and pass-by-reference functionalities and their implementations are quite simple. Please compile and run the below codes and you will know what I am saying.

class A

{

private int a;

public A()

{

a = 0;

}

public void set_a( int i )

{

a = i;

}

public int get_a()

{

return( a );

}

}

public class abc

{

private A aa, aaa;

public abc()

{

aa = new A();

aa.set_a( 6 );

aaa = new A();

aaa.set_a( 9 );

System.out.println( "Before PassByValue(), aa's value is " + aa.get_a() );

PassByValue( aa );

System.out.println( "After PassByValue(), aa's value is " + aa.get_a() );

System.out.println( "Before PassByRef(), aaa's value is " + aaa.get_a() );

PassByRef( aaa );

System.out.println( "After PassByRef(), aaa's value is " + aaa.get_a() );

}

public void PassByValue( A aaaa )

{

A new_a = new A();

new_a.set_a( 5 );

aaaa = new_a;

}

public void PassByRef( A aaaa )

{

aaaa.set_a( 5 );

}

public static void main( String args[] )

{

new abc();

}

}

liaodongyana at 2007-7-12 20:03:35 > top of Java-index,Java Essentials,Java Programming...
# 9
> WOW! Did you make that up yourself?Ya man
georgemca at 2007-7-12 20:03:35 > top of Java-index,Java Essentials,Java Programming...
# 10
> All of you are totally wrong!!!You are wrong. I haven't even bothered reading your codez, because you are wrong, plain and simple. This is not debatable
georgemca at 2007-7-12 20:03:35 > top of Java-index,Java Essentials,Java Programming...
# 11

> All of you are totally wrong!!!

>

> ...

No, YOU are wrong.

To quote James Gosling (he is the inventor of Java!):

"There is exactly one parameter passing mode in Java -- pass by value -- and that helps keep things simple."

James Gosling, "The Java Programming Language, Second Edition"

Now make way for the flame-throwers!

; )

prometheuzza at 2007-7-12 20:03:35 > top of Java-index,Java Essentials,Java Programming...
# 12
> All of you are totally wrong!!!> *annoying buzzer sound* No, I'm sorry that's not the correct answer but thank you for playing.You're forgetting the memory heap. Go back and rethink what you've done here and what it really means.PS.
puckstopper31a at 2007-7-12 20:03:35 > top of Java-index,Java Essentials,Java Programming...
# 13
> > WOW! Did you make that up yourself?> > Ya manDatShitDontFitException; )
prometheuzza at 2007-7-12 20:03:35 > top of Java-index,Java Essentials,Java Programming...
# 14

> All of you are totally wrong!!!

>

> Actually, Java has both pass-by-value and

> pass-by-reference functionalities and their

> implementations are quite simple. Please compile and

> run the below codes and you will know what I am

> saying.

No, it's actually you who's wrong. You're confusing a reference and it's object. When you "pass an object" to a method, you're actually just passing a copy of the reference that points to that object. The object itself doesn't get passed. Since the reference that now exists in the method body points to the same object, you can change the object however you want and then see those changes outside of the method. You can't however change the reference itself, which you could with pass by reference.

Have you read these?

http://www.javaranch.com/campfire/StoryPassBy.jsp

http://www.javaranch.com/campfire/StoryCups.jsp

hunter9000a at 2007-7-12 20:03:35 > top of Java-index,Java Essentials,Java Programming...
# 15

> All of you are totally wrong!!!

>

> Actually, Java has both pass-by-value and

> pass-by-reference

Nope.

Java is always pass-by-value. Always.

Always. Always. Always.

http://javadude.com/articles/passbyvalue.htm

http://java.sun.com/developer/JDCTechTips/2001/tt1009.html#tip1

http://www.javaranch.com/campfire/StoryPassBy.jsp

http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

http://www-106.ibm.com/developerworks/library/j-praxis/pr1.html

http://www.cs.toronto.edu/~dianeh/tutorials/params/

http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#38698

http://radio.javaranch.com/channel/val/2004/05/21/1085125887000.html

There is exactly one parameter passing mode in Java -- pass by value -- and that helps keep things simple.

-- James Gosling, "The Java Programming Language, Second Edition"

(James Gosling being the father of Java)

jverda at 2007-7-21 22:36:18 > top of Java-index,Java Essentials,Java Programming...
# 16

> All of you are totally wrong!!!

oh... my... god... what a shock

> Actually, Java has both pass-by-value and

> pass-by-reference functionalities and their

> implementations are quite simple. Please compile and

> run the below codes and you will know what I am

> saying.

it could be funny to see you explain your own codes and what you think they show. it seems to be a recurring theme that the (2) pbr people i've seen on this forum so far, are unable to offer more than "blahblahblah, there you see"

OnBringera at 2007-7-21 22:36:18 > top of Java-index,Java Essentials,Java Programming...
# 17

Objects, are always passed by reference as someone has already mentioned in this thread. Changing the attributes of an object in a method called, would result in the parent object being modified.

Let's not keep on referring to what is given in some thread or book or tutorial. I just wrote a small example and the results confirmed the pass by reference concept. Please go ahead and write a sample program on your own and confirm.

SourabhGoeLa at 2007-7-21 22:36:18 > top of Java-index,Java Essentials,Java Programming...
# 18

> Actually, Java has both pass-by-value and pass-by-reference functionalities...

Nope. If it had pass-by-reference, you could change the value of the original variable from within the method. But you can't change the value of the original variable in Java (modifying an object's internal data is not changing the contents of the original variable) , because Java only has pass-by-value semantics.

~

yawmarka at 2007-7-21 22:36:18 > top of Java-index,Java Essentials,Java Programming...
# 19

> Objects, are always passed by reference as someone

> has already mentioned in this thread.

Some people will say incorrectly that objects are passed "by reference."

-- Arnold, K., Gosling J., Holmes D. (2006). The Java Programming Language Fourth Edition. Boston: Addison-Wesley.

Answer that.

> Let's not keep on referring to what is given in some

> thread or book or tutorial. I just wrote a small

> example and the results confirmed the pass by

> reference concept. Please go ahead and write a sample

> program on your own and confirm.

We have. There is no way to demonstrate pass-by-reference semantics in Java because it doesn't exist. You're confusing passing a reference with pass-by-reference. They're not the same thing. What you have demonstrated is pass-by-value semantics. The ability to modify an object via a pointer has nothing to do with how the parameter is passed.

~

yawmarka at 2007-7-21 22:36:18 > top of Java-index,Java Essentials,Java Programming...
# 20

> Objects, are always passed by reference as someone

> has already mentioned in this thread.

Saying that sentence here is just like going to a astronomy convention and saying "The Sun revolves around the Earth, this has been established already, I wish you people would stop contradicting me!"

hunter9000a at 2007-7-21 22:36:18 > top of Java-index,Java Essentials,Java Programming...
# 21

> > Objects, are always passed by reference as

> someone

> > has already mentioned in this thread.

>

>

> Some people will say incorrectly that objects are

> passed "by reference."

> -- Arnold, K., Gosling J., Holmes D. (2006). The

> Java Programming Language Fourth Edition.

> Boston: Addison-Wesley.

Look man, when are you going to stop using your "references" and "facts" and "reality" as a crutch and just accept the truth?!

(It's best if you picture Chris Farley doing the over-exaggerated finger quotes while you read that)

hunter9000a at 2007-7-21 22:36:18 > top of Java-index,Java Essentials,Java Programming...
# 22

> Objects, are always passed by reference as someone

> has already mentioned in this thread. Changing the

> attributes of an object in a method called, would

> result in the parent object being modified.

>

> Let's not keep on referring to what is given in some

> thread or book or tutorial. I just wrote a small

> example and the results confirmed the pass by

> reference concept. Please go ahead and write a sample

> program on your own and confirm.

Please explain the output of this code, will you?

public class Main {

static void foo(String s) {

s = "B";

}

public static void main(String[] args) {

String s = "A";

System.out.println("Before -> "+s); // prints "A"

foo(s);

System.out.println("After -> "+s); // prints "A"

}

}

prometheuzza at 2007-7-21 22:36:18 > top of Java-index,Java Essentials,Java Programming...
# 23

Here's an example of actual pass-by-reference in C#:public class Foo

{

public static void Main()

{

string s = "foo";

Console.WriteLine(s); // foo

passByRef(ref s);

Console.WriteLine(s); // bar

}

public static void passByRef(ref string s)

{

s = "bar";

}

}

The same thing is not possible in Java, because Java only has one type of parameter passing mechanism: pass-by-value.

~

yawmarka at 2007-7-21 22:36:18 > top of Java-index,Java Essentials,Java Programming...
# 24

> in the next version of java, codenamed fo' jizzle,

> they're going to introduce a new feature called

> drive-by reference whereby objects on the heap and

> references in scope will be randomly matched up to

> each other by an undisclosed and non-deterministic

> algorithm, in a big car and baseball cap, known as

> Pimp Daddy Heap. this is so that any old skool

> developers who think java is getting a little safe

> and boring can now convince themselves they're

> "keepin' it real J-side". the downside, of course,

> being that for us confused uncles, we'll be forever

> at the mercy of ClassCastExceptions ( to be renamed

> "DatShitDontFitException" ) when our references keep

> pointing to the wrong things, and

> NullPointerExceptions ( or

> "FoolDoneGoneLikeElvisExceptions" ) when there aren't

> enough objects to go round

this is the best thing to come out of all these PBR threads!

mkoryaka at 2007-7-21 22:36:18 > top of Java-index,Java Essentials,Java Programming...
# 25

Here is proof that pass by ref does not exist in Java. If it would, the result would be :

one

two

two

two

public class PassByRef {

public String name;

public PassByRef(String n) {

name = n;

}

public static void switchRefs(PassByRef a, PassByRef b) {

a = b;

}

public String toString() {

return name;

}

public static void main(String args[]) {

PassByRef a = new PassByRef("one");

PassByRef b = new PassByRef("two");

System.out.println(a);

System.out.println(b);

PassByRef.switchRefs(a, b);

System.out.println(a);

System.out.println(b);

}

}

but the switch never changes the reference in main.

Message was edited by:

Raphael.Blanchard

Raphael.Blancharda at 2007-7-21 22:36:18 > top of Java-index,Java Essentials,Java Programming...
# 26
Right, can we please now put this topic to bed forever?
georgemca at 2007-7-21 22:36:18 > top of Java-index,Java Essentials,Java Programming...
# 27

> Objects, are always passed by reference

No. References are passed by value. Read the bloody liniks. Read the quote from the guy who invented the language.

> has already mentioned in this thread. Changing the

> attributes of an object in a method called, would

> result in the parent object being modified.

That's not PBR.

void foo(Bar bar) {

bar = somethingElse; // If the caller sees this, that's PBR.

bar.setSomething(baz); // The caller seeing this change does NOT mean PBR

bar.something = qux; // The caller seeing this change does NOT mean PBR

> Let's not keep on referring to what is given in some

> thread or book or tutorial.

What's given in those many references and by the inventor of the language and in the JLS is correct.

> I just wrote a small

> example and the results confirmed the pass by

> reference concept.

No, it confirms that you don't understand what PBR means.

jverda at 2007-7-21 22:36:18 > top of Java-index,Java Essentials,Java Programming...
# 28

> > > Objects, are always passed by reference as

> > someone

> > > has already mentioned in this thread.

> >

> >

> > Some people will say incorrectly that objects are

> > passed "by reference."

> > -- Arnold, K., Gosling J., Holmes D. (2006). The

> > Java Programming Language Fourth Edition.

> > Boston: Addison-Wesley.

>

> Look man, when are you going to stop using your

> "references" and "facts" and "reality" as a crutch

> and just accept the truth?!

>

> (It's best if you picture Chris Farley doing the

> over-exaggerated finger quotes while you read that)

I actually pictured Homer Simpson. It was quite effective. :-)

jverda at 2007-7-21 22:36:19 > top of Java-index,Java Essentials,Java Programming...
# 29
> Right, can we please now put this topic to bed> forever?Wishful thinking.
jverda at 2007-7-21 22:36:19 > top of Java-index,Java Essentials,Java Programming...
# 30
> > Right, can we please now put this topic to bed> > forever?> > Wishful thinking.I know, but I got a new car, a promotion, a pay rise, a bonus and a job offer all within 24 hours this week, so I'm thinking my luck might just hold out :-)
georgemca at 2007-7-21 22:36:24 > top of Java-index,Java Essentials,Java Programming...
# 31
Im no expert at this but i found a nice article which explains it: http://www.ibm.com/developerworks/java/library/j-praxis/pr1.html
Overkilla at 2007-7-21 22:36:24 > top of Java-index,Java Essentials,Java Programming...
# 32
> Im no expert at this but i found a nice article which> explains it:See reply #3 (URL #5).I also recommend the link in reply #2 (also in #3).~
yawmarka at 2007-7-21 22:36:24 > top of Java-index,Java Essentials,Java Programming...