pass by value?!

I know that java handles Object as reference and it is a pass-by-reference language. So I would know why the following program will prints out 0 instead of 10

public class test {

public static void givemeten(Integer x) {

x = new Integer(10);

}

public static void main(String args[]) {

Integer y = new Integer(0);

test.givemeten(y);

System.out.println(y);

}

}

It would be great that if anyone can answer this question.

[490 byte] By [wink_henrya] at [2007-10-2 9:45:43]
# 1

> I know that java handles Object as reference and it

> is a pass-by-reference language.

Java is always pass-by-value. Period. Repeat after me: Pass-by-value.

Now, Java does use the term "reference" to describe variables that are used for accessing objects, as opposed to primitives. And when you "pass an object" to a method, you're really passing a copy of a reference to that object. But passing a copy of a reference is passing that reference by value.

Despite the apparent intuitively obvious association, passing a reference by value is not--I repeat: NOT--the same as passing by reference. Don't even go there. It's not.

Java is 100% pure, unadulterated pass-by-value. Passing a reference by value is not the same as pass-by-reference.

No, you can't "just call it pass-by-reference to keep it simple." Pass-by-reference has a very specific meaning in computer science, and that is not consistent with how Java passes references by value. Using the term incorrectly will not "keep it simple." It will muddy the issue.

class Foo {

String str_;

}

...

void m1(String str) {

str = "abc";

}

void m2(Foo foo) {

foo.str_ = "xyz";

}

String s = "ORIGINAL";

Foo f = new Foo();

foo.str_ = "ORIGINAL";

m1(s);

m2(f);

System.out.println(s); // prints "ORIGINAL"

System.out.println(f.str_); // prints "xyz"

For further explanations of what pass-by-value is and why Java is pass-by-value, see the following:

[url=http://javadude.com/articles/passbyvalue.htm]http://javadude.com/articles/passbyvalue.htm[/url]

[url=http://java.sun.com/developer/JDCTechTips/2001/tt1009.html#tip1]http://java.sun.com/developer/JDCTechTips/2001/tt1009.html#tip1[/url]

[url=http://www.javaranch.com/campfire/StoryPassBy.jsp]http://www.javaranch.com/campfire/StoryPassBy.jsp[/url]

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

[url=http://www-106.ibm.com/developerworks/library/j-praxis/pr1.html]http://www-106.ibm.com/developerworks/library/j-praxis/pr1.html[/url]

[url=http://www.cs.toronto.edu/~dianeh/tutorials/params/]http://www.cs.toronto.edu/~dianeh/tutorials/params/[/url]

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

> So I would know why

> the following program will prints out 0 instead of

> 10

Because Java passes the reference by value. There are two independent references that have the same value--the address of that Integer. When you create a new Integer and point the reference inside the method at it, it doesn't affect the reference outside the method.

jverda at 2007-7-16 23:51:13 > top of Java-index,Java Essentials,New To Java...
# 2

> I know that java handles Object as reference and it

> is a pass-by-reference language.

That's wrong ... java always uses pass-by-value. There have been quite some discussions with people who didn't believe that, but it is true in the end:

http://www.google.com/search?q=java%20uses%20pass%20by%20value

JoachimSauera at 2007-7-16 23:51:13 > top of Java-index,Java Essentials,New To Java...
# 3

> > I know that java handles Object as reference and

> it

> > is a pass-by-reference language.

>

> Java is always pass-by-value. Period. Repeat after

> me: Pass-by-value.

D'Oh! I'm slower and less helpful. Guess it's my time to be the slowest old sod today (unless someone is willing to out-slow me ;-)).

JoachimSauera at 2007-7-16 23:51:13 > top of Java-index,Java Essentials,New To Java...
# 4

> > > I know that java handles Object as reference and

> > it

> > > is a pass-by-reference language.

> >

> > Java is always pass-by-value. Period. Repeat after

> > me: Pass-by-value.

>

> D'Oh! I'm slower and less helpful. Guess it's

> my time to be the slowest old sod today (unless

> someone is willing to out-slow me ;-)).

I just saw a post by Jos a few minutes ago. He's usually pretty good at making others look fast by comparison. ;-)

jverda at 2007-7-16 23:51:13 > top of Java-index,Java Essentials,New To Java...
# 5

> > > > I know that java handles Object as reference

> and

> > > it

> > > > is a pass-by-reference language.

> > >

> > > Java is always pass-by-value. Period. Repeat

> after

> > > me: Pass-by-value.

> >

> > D'Oh! I'm slower and less helpful. Guess

> it's

> > my time to be the slowest old sod today (unless

> > someone is willing to out-slow me ;-)).

>

> I just saw a post by Jos a few minutes ago. He's

> usually pretty good at making others look fast by

> comparison. ;-)

LOL :)

kajbja at 2007-7-16 23:51:13 > top of Java-index,Java Essentials,New To Java...
# 6
> I know that java handles Object as reference and it> is a pass-by-reference language. Java is always, always, always pass-by-value. Even James Gosling says so.(This is for Joachim. ;) )
CeciNEstPasUnProgrammeura at 2007-7-16 23:51:13 > top of Java-index,Java Essentials,New To Java...
# 7

> > > > I know that java handles Object as reference and it

> > > > is a pass-by-reference language.

> > >

> > > Java is always pass-by-value. Period. Repeat after

> > > me: Pass-by-value.

> >

> > D'Oh! I'm slower and less helpful. Guess it's

> > my time to be the slowest old sod today (unless

> > someone is willing to out-slow me ;-)).

>

> I just saw a post by Jos a few minutes ago. He's usually pretty good

> at making others look fast by comparison. ;-)

Duh; I have a valid excuse today: I forgot to fire up my old espresso machine

this morning, so I'm only just now sipping my first drops of fluid caffeine.

I have a feeling that this day is ruined already ;-)

kind regards,

Jos (is it 0:00pg yet?)

JosAHa at 2007-7-16 23:51:13 > top of Java-index,Java Essentials,New To Java...
# 8
Thanks for everyone to clear my concept and the quick response >_*
wink_henrya at 2007-7-16 23:51:13 > top of Java-index,Java Essentials,New To Java...
# 9
know about the latest version os java http://www.javapeople.blogspot.com
dipalia at 2007-7-16 23:51:14 > top of Java-index,Java Essentials,New To Java...
# 10
> know about the latest version os java> > http://www.javapeople.blogspot.comStop spamming.
CeciNEstPasUnProgrammeura at 2007-7-16 23:51:14 > top of Java-index,Java Essentials,New To Java...
# 11
Pay no need to DaFei's ramblings (they will only confuse you). However, the rest of the discussion is highly enlightening (say from post 200 on): http://forum.java.sun.com/thread.jspa?threadID=689316- Saish
Saisha at 2007-7-16 23:51:14 > top of Java-index,Java Essentials,New To Java...