This content has been marked as final. Show 6 replies
Welcome to the forum!
On the other hand I am studying on Thinking in Java by Bruce Eckel where it is stated that Java - apart from primitives - pass objects by reference.
So where is the truth? Where can I find a detailed explaination?
You already have the truth in what James Gosling, the creator of Java, said. You either misstated what the other reference said or it is wrong.
What Mr. Gosling said is also represented in the Java Language Specification
See section 8.4.1
When the method or constructor is invoked (§15.12), the values of the actual argument expressions initialize newly created parameter variables, each of the declared type, before execution of the body of the method or constructor.
Note that the above applies to argument expressions of ALL types: primitive or object. The value of the 'newly created parameter variable' is a COPY (by value) of the actual argument expression.
Here is a JavaWorld article with examples that should help clear things up.
Edited by: rp0428 on Sep 8, 2012 9:11 AM - added additional comment after researching OP's statement.
Just as I suspected you misstated what the reference said. I could not find a reference that used exactly your wording
Thinking in Java by Bruce Eckel where it is stated that Java - apart from primitives - pass objects by reference.
But I did find this reference to his book
And in that reference on page 1026 of his book he has similar words but you have taken them completely out of context.
That section is titled 'Pass by value' and the author clearly states
there are two fairly distinct camps:
1. Java passes everything by value.
. . .
2. Java passes primitive by value (no argument there), but objects are passed by reference.
. . .
Item 2 above is presumably where you got your statement that you included in your post.
But the author clearly states in the following paragraph that he is sidestepping the issue.
Having given both camps a good airing, and after saying "It depends on how you think of a reference," I will attempt to sidestep the issue. In the end, it isn't that important - what is important is that you understand that passing a reference allows the caller's object to be changed unexpectedly.
Edited by: rp0428 on Sep 8, 2012 9:11 AM
Hey, wait a minute - maybe I have understood finally.
Thing is: everything in Java is an Object, and all the objects are passed by reference. BUT, references themselves are passed by value - and not by another reference as it happens, for example, in C / C++ with the pointers when you can pass pointers by reference letting them to be modified. In Java references - pointers, let's say, are passed by value, so it means a copy of them is created and passed to a method. This method, then, can alter the copy but not the original, which remains untouched (this avoids, among other, some security issues where a virus can modify a pointer passed by reference making it point to a different object).
Is that correct?
Edited by: Giulio Severini on 9-set-2012 3.02
Java passing by reference, where a good detailed explaination?There is no such thing in Java, so there is no explanation either, detailed or otherwise. There is nothing to explain.
Hey, EJP, can you please have a look at my self-answer, if it is correct or not?
everything in Java is an Object, and all the objects are passed by reference.
Wrong - Objects are not passed at all so they can't be passed by reference or value.
What is passed is a reference to an Object and as you just said
references themselves are passed by value
everything in Java is an ObjectNo.
and all the objects are passed by reference.No.
references themselves are passed by valueYes.