4 ms·
I wonder if there is any kind of authoritative definition of these words that would apply to Java, C#, C++, and other languages. I suspect not. Compared to C++
by mbrock 9y ago
I wonder if there is any kind of authoritative definition of these words that would apply to Java, C#, C++, and other languages. I suspect not.
Compared to C++, Java object variables behave more like pointers.
- danbruc 9y agoReferences are opaque handles for values, you can just deference them and compare them for equality. Pointers are not opaque, their values are address and you can perform arithmetic on them. With that in mind C++ references are pretty much like references in Java or C#. The difference is that C++ additionally does not allow null references and reassignment, but that is more of a statement about the variable holding the reference than about the reference itself. And because reference variables are readonly after initialization it may feel like C++ pointers are closer to Java or C# references than C++ references, and for practical purposes this is certainly true, but with regard to the relevant difference of references being opaque, C++ references are actually closer to Java or C# references than C++ pointers.
- nordsieck 9y ago> References are opaque handles for values, you can just deference them and compare them for equality. Pointers are not opaque, their values are address and you can perform arithmetic on them. What about Go? It claims to have pointers. You can't do arithmetic on them, but you can take the address of a pointer so you can do all the cute double dereference list and tree algorithms.
- danbruc 9y agoI never used Go in any meaningful way so I just had to look at the documentation. I lean towards classifying it as references instead of pointers despite the name. References to references are perfectly valid even though they are not available in all languages with references. On the other hand Go exposes an implementation detail, the fact that references are implemented as addresses. This is probably true for most languages with references but they usually keep this an implementation detail so that it would be perfectly valid to implement references with something other then addresses. But because you can not really take advantage of the fact that references are implemented using addresses, you can not manipulate them and it seems you can not even calculate the difference between two pointers unless you go through unsafe, I would call it references [with slightly but inconsequentially exposed implementation]. As said, this is based on a ten minute read through the first couple of search results and I probably have not even hit any official documentation, so this might very well miss something important and be wrong.