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
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
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();
}
}
> 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!
; )
> 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
> 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)
> 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"
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.
> 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.
~
> 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.
~
> 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!"
> > 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)
> 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"
}
}
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.
~
> 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!
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
> 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.
> > > 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. :-)