Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java passing by reference, where a good detailed explaination?

GiulioSeveriniSep 8 2012 — edited Sep 9 2012
Hi,

I am new to the forum and I have a question for you. I am trying to properly understand the Java passing by reference but I am getting a little bit confused.
Does Java pass by reference or by value?
I have a quote from James Gosling stating as following:

Some people will say incorrectly that objects are passed “by reference.” In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory…. The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode — pass by value — and that helps keep things simple.

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?

Thank you and kind regards,

Giulio.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 7 2012
Added on Sep 8 2012
6 comments
5,582 views