7 ms·
>Java is strictly pass-by-value, exactly as in C In C you can pass a pointer, and a pointer to a pointer. I understand both cases are usually called "pass-by-
by lucio 12y ago
>Java is strictly pass-by-value, exactly as in C
In C you can pass a pointer, and a pointer to a pointer.
I understand both cases are usually called "pass-by-reference". If you call "pass-by-value" to passing a pointer, then the notion of "pass-by-value" and "pass-by-reference" are not very useful.
IMHO "pass-by-reference" means you can mutate the attributes of the thing passed (as in java when you pass an object) and also means you can be operating on a "volatile" area of memory, meaning another thread can CHANGE the data while the function is executing.
OTOH "pass-by-value" means you get a "snapshot" of the value at the point of the function-call, the copy is normally on the stack, and no other thread will change the data while the function is running. Also you can mutate the area with gusto without worrying of data-races, side-effects or altering the calling function variables.
This two "definitions" of pass-by-value and pass-by-reference are the ones I find useful regarding concurrency and side-effects.
If you tell me that passing an integer and passing an object are both the same thing (pass-by-value) in Java, then I will tell you that pass-by-value and pass-by-reference are not very useful concepts.
- argonaut 12y agoIt is a useful concept because there are languages with pass-by-reference constructs. C++, Swift (sort of, with inout parameters), are two examples that come to mind. Java and C are simply not those languages.
- cube00 12y agoYou can pass double pointers in C so functions can change the original pointer's address value. You can't do that in Java.
- ta82828 12y agoYes, you can do that in C, but that is still not pass by reference. In C you are not passing the variable you are trying to change, you are passing a pointer to it. You cannot write swap(a, b); You have to write swap(&a, &b);
- lucio 12y agoThe problem is that you will label both cases as an example of "pass-by-value", while they're fundamentally different. That's why people call "pass-by-reference" the second example.
- TazeTSchnitzel 12y agoThey're not fundamentally different. The passing semantics are the same in both cases, the variable bound within the body of swap will not be the same variables we are passing it. The difference is that the value being passed is a pointer in the second case.
- ta82828 12y agoThey are both labelled pass-by-value because they are both pass-by-value. Yes, they are different, but that doesn't mean one is pass-by-reference and the other is pass-by-value. The C example is equivalent to pa = &a; pb = &b; swap(pa, pb); When swap returns, the values of pa and pb are unchanged. Only the values or a and b are changed. But a and b were not passed to swap. Therefore this is not pass-by-reference, but rather passing a pointer to a variable by value.
- lucio 12y agoI fully understand the difference, and I know that you're technically correct. What I'm saying is that people tend to abbreviate "pass-a-reference-by-value" to "pass-by-reference" because the differences between passing a value (value snapshot) and passing a reference-value (passing the address-of) are way more important* than if the language syntax allows you to pass an "implicit" variable reference or not. (the swap example) *"more important" for code-reading, code-analysis, code-safety analysis, and also even more important in a concurrent environment because of side-effects and data-races. Maybe the feature should be called "pass an implicit reference", which describes better what Java does, instead of saying "everything is pass-by-value" which is correct but, as the evidence shows (the mere existence of the article), it is very confusing for "folks". >I'm really tired of hearing folks (incorrectly) state "primitives are passed by value, objects are passed by reference".
- tedunangst 12y agoIn Java, you stick the pointer into a one element array and pass that. Given some flexibility about the definition of the "original pointer", it can be done, although there's no special syntax for it. Or maybe "array syntax" is "pass by reference" syntax.
- Gibbon1 12y agoI'm remembering at least some versions of basic were pass by reference for all types.
- boomlinde 12y agoEarly variants simply "passed by reference" by virtue of not having local variables or functions at all. A "function" would be implemented by the programmer as a subroutine that used a specific set of the global variables available for its input and output values.
- ta82828 12y agoCall/pass by reference means you can write a function that swaps the values of two variables such that the caller will see the values swapped. See the examples in the posted article for Pascal and C++. This cannot be done in Java or C, which is why they are not pass by reference.
- m_mueller 12y ago> IMHO "pass-by-reference" means you can mutate the attributes of the thing passed (as in java when you pass an object) and also means you can be operating on a "volatile" area of memory, meaning another thread can CHANGE the data while the function is executing. Sorry, but having an 'opinion' about such definitions doesn't help. Just [use the definition](http://en.wikipedia.org/wiki/Evaluation_strategy#cite_note-1 http://en.wikipedia.org/wiki/Evaluation_strategy#cite_note-1). This clearly states what the article tried to explain with helpful examples. > If you tell me that passing an integer and passing an object are both the same thing (pass-by-value) in Java, then I will tell you that pass-by-value and pass-by-reference are not very useful concepts. They are useful, just not in the way that you think of them. You could use these definitions to have a meaning in terms of side-effects, but only if you design your objects accordingly, i.e. there are no exposed methods to do an in-place change of your object's data, instead you only have class functions that return a new object and read-only accessor methods. Combining this with a language that is strictly pass-by-value (such as Java and a subset of C++ without reference parameters) should make your program side-effect free.
- Veedrac 12y ago> Sorry, but having an 'opinion' about such definitions doesn't help. Just [use the definition](http://en.wikipedia.org/wiki/Evaluation_strategy#cite_note-1 http://en.wikipedia.org/wiki/Evaluation_strategy#cite_note-1). This clearly states what the article tried to explain with helpful examples. I don't think that's a very strong argument. For one, it's not really useful to be totally prescriptivist, especially when talking about just-birthed fields like computing. Also, consider the usage on that page of "data structure" where it's implied that you can't have a stack-allocated data structure (namely that data structures aren't value types, even those containing pointers). Such a distinction seems remarkably unusual to me, indicating that it's best not to take that page as gospel. Personally I prefer to avoid these terms altogether because on the rare case that it would be useful to use the words, it's rarely worth the inevitable argument.
- m_mueller 12y agoI didn't mean to use that wiki as gospel, but the book citation I linked seems to hold up. And for a concept like pass-by-value vs. pass-by-reference, I don't think it's a good idea to go and come up with your own definitions if you don't have a very good reason to do so. While I agree that computing is a still a young field, and lots of stones are still rolling, it's still important to establish some common language and models just in order to communicate ideas. This includes not overloading terms that have been clearly established by now. If you find a well-known source[1] that would disagree with the terminology as cited, I'd be happy to revise my opinion. [1] such as Kay, Ritchie, Dijkstra, Engelbart or even Matsumoto, Van Rossum, Eich, Crockford.