3 ms·
I've seen this topic rehashed repeatedly since I started coding Java in 1999. It has always been that Java is passed by value. https://news.ycombinator.com/it
by sesteel 8y ago
I've seen this topic rehashed repeatedly since I started coding Java in 1999. It has always been that Java is passed by value.
https://news.ycombinator.com/item?id=9246356 https://news.ycombinator.com/item?id=9246356
and
<q> 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. -- James Gosling, et al., The Java Programming Language, 4th Edition </q>
- lmm 8y agoThe answer Gosling actually gives in your link is "Depends on your terminology"; he acknowledges it "behaves like pass-by-reference". You say "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" - but this is the behavior of Java. Gosling talks about "the pointer" because he's thinking in terms of the implementation, but that can't be the correct answer in the context of Java because pointers don't exist in the language.
- itronitron 8y agoright, so the object is passed by reference, and the object reference is passed by value...