Objects passed by reference or value?
Hi everyone, I just want to get something off my chest. From day one, I had always thought that objects passed into parameters are passed as reference. For example,
class X
{
}
publicclass Y
{
Y()
{
X x1 =null;
System.out.println(x1);
test(x1);
}
privatevoid test(X x1)
{
System.out.println(x1);
}
publicstaticvoid main(String[] args)
{
new Y();
}
}
The output would be..
X@10b62c9
X@10b62c9
HOWEVER, I came across an article at http://www.yoda.arachsys.com/java/passing.html telling me that objects are passed by value. So, which is correct? Are objects in Java really passed by value?
[1332 byte] By [
MJI83a] at [2007-10-2 6:39:29]

Correction from previous postY(){X x1 =new X();System.out.println(x1);test(x1);}
MJI83a at 2007-7-16 13:47:39 >

> Are objects in Java really passed by value?Yes, a copy of the reference to the Object is made and passed to the method. Therefore you can still access the Object directly. This topic has been discussed numerous times (too many to count) in the forum.
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
"Objects passed by reference or value?"
Neither. Objects are not passed.
Primitives are passed by value.
Reference types are passed by value.
The null type is passed by value.
Objects are not passed at all.
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)
> Java is always pass-by-value. Always.> > Always. Always. Always.etc. etc.Hello vanilla_lorax, new name?
> Hello vanilla_lorax, new name?Looks like it. The posts always seemed familiar... this was the proof. :)
Quoting targaryen:> Reference types are passed by value.Isnt that an indirect way of passing objects by reference? Eventually, the original objects gets modified.
MJI83a at 2007-7-16 13:47:39 >

> Quoting targaryen:
> > Reference types are passed by value.
>
> Isnt that an indirect way of passing objects by
> reference? Eventually, the original objects gets
> modified.
No, it's not the same.
public static void main(String[] args) {
String s = "some text";
change(s);
System.out.println(s); //This still prints "some text"
}
private static void change(String s) {
s = "another text";
}
Kaj
kajbja at 2007-7-16 13:47:39 >

> Quoting targaryen:
> > Reference types are passed by value.
>
> Isnt that an indirect way of passing objects by
> reference? Eventually, the original objects gets
> modified.
No. You cannot replace the referenced object by another one. Were it passed by reference you could.
That's because the reference itself is passed by value and therefore immutable.
> public static void main(String[] args) {
>String s = "some text";
>change(s);
> System.out.println(s); //This still prints "some
> some text"
> }
>
> private static void change(String s) {
>s = "another text";
> }
Like I was saying before, the original object gets modified.
Your code analogy does not relate to what I am trying to bring across.
In the line of code s = "another text", this code constructs a new String class. However, I am only relating only to one class.
I will still stress on indirect objects passing by reference by showing this example:
class X
{
int i;
}
class Y
{
Y()
{
X x1 = new X();
x1.i = 1;
System.out.println(x1.i);
test(x1);
System.out.println(x1.i);
}
private void test(X x1)
{
x1.i = 5;
}
public static void main(String[] args)
{
new Y();
}
}
output is...
1
5
Yes, I acknowledged that Java passes reference types by value, which also means indirect passing by reference.
MJI83a at 2007-7-16 13:47:40 >

Not this discussion again...
> Yes, I acknowledged that Java passes reference types
> by value, which also means indirect passing by
> reference.
I don't know what you fail to understand in post #4 by targaryen. This has been discussed about 10983481 times in this forum, and it's rather boring to discuss it again. Can't you just search the forum?
The correct answer is that it's always always always pass by value. To say anything else is wrong.
Kaj
kajbja at 2007-7-16 13:47:40 >

if the term you wish to use is "indirect passing by reference," whatever, we're not the language police. But do bear in mind that the standard terminology that everyone has agreed to use in these forums is that "objects are not passed" and that "references are passed by value." It's been agreed that it's wrong to say any form of "objects are passed by reference" because "pass-by-reference" has a very strict definition in computer science.
some have said that saying "objects are passed by reference-value" (that is, by the value of their reference) would better describe the situation, but that hasn't caught wide-spread use... mainly because it still suggests that objects are somehow moved from one place to another when they're not.