5 ms·
You might think that the value of foo is that which is compared when evaluating foo == bar, which is a pointer!
by mbrock 9y ago
You might think that the value of foo is that which is compared when evaluating foo == bar, which is a pointer!
- lmm 9y ago> You might think that the value of foo is that which is compared when evaluating foo == bar Few if any working Java programmers do think like that though. Like, if you have String x and String y, and println(x); prints "foo" and println(y) prints "foo" and x != y (a perfectly ordinary situation to get into in Java), we would still say "the value of x is "foo"".
- mbrock 9y agoEquality on strings is a common source of confusion in Java, and I think that's because many newbies aren't properly taught the kind of semantic pedantry we're discussing... and because the language design is weird. Strings pretend to be immutable value objects, but they still have object identity, which doesn't really make sense as far as I can tell. It still seems to me that the only good definition of an object variable's "value" in Java is its pointer.
- lmm 9y ago> Strings pretend to be immutable value objects, but they still have object identity, which doesn't really make sense as far as I can tell. Yeah. Java doesn't have proper value objects, there's these vestigal referencey effects everywhere, probably because of the language being designed for much more memory-constrained environments than those where it's actually used today. Still, most of the time you can just ignore that, remember a few simple rules like "always use .equals rather than ==" and you're good. > It still seems to me that the only good definition of an object variable's "value" in Java is its pointer. Good for technical pedantry maybe. Good for working programming, no - 99.9% of the time when you're talking about the "value" (not in terms of function-passing semantics, just generally) you want to talk about "foo", not about "an object reference to address 54fc892d".
- mbrock 9y agoTechnical pedantry could almost be a definition of programming...
- kbp 9y ago> > It still seems to me that the only good definition of an object variable's "value" in Java is its pointer. > Good for technical pedantry maybe. Good for working programming, no - 99.9% of the time when you're talking about the "value" (not in terms of function-passing semantics, just generally) you want to talk about "foo", not about "an object reference to address 54fc892d". This is almost exactly the same as pointers in just about every language that has them, though. You pass pointers around, copy them, and dereference them (which includes struct member access and array indexing), and that's about it. C is a little different because it somewhat conflates arrays with pointers to arrays, but that's hardly reason to decide that pointers are totally different. In C, you have a variable 'foo' whose type is 'T *' that when cast to an int happens to equal 0x54fc892d. The only abstraction mechanism you're asking for is the ability to store pointers in variables, it's nothing to do with their type. They're handles on objects, they're called pointers because they're values that 'point' to another value somewhere else, they're exactly what Java object variables are.
- lmm 9y ago(For ^ read *, thanks HN) The difference is that in C you can talk about x being a char^^ value "pointer to address 54fc892d" because you can also talk about ^x being a char^ value "foo". In Java there is no ^x, so the only thing you can talk about when you want to talk about "foo" is "the value of x", and you want to talk about "foo" far more often than you want to talk about "pointer to address 54fc892d".
- deleted 9y ago[deleted]
- kbp 9y agoWell, that's just the little difference I mentioned about C conflating arrays and pointers to them. The char ^ is being viewed as an array, which in C happens to be synonymous with a pointer to the start of the array. It's certainly not inherent to pointers that they be mixed up with arrays like that (or to strings that they be represented as char arrays). As an aside, C's conflation of arrays and pointers was done because they're very closely related, and you can get very similar results to a C pointer-to-pointer by wrapping an object in a one-element array. In C, ^x is equivalent to x[0], and Java doesn't have ^x, so you can just use x[0]. More generally, though, a C programmer is virtually never thinking of 'the address 54fc892d': unless they're doing really low-level stuff, they won't think of it like that any more than a Java programmer, even using pointers all day, just like the Java programmer is; that's what I don't get about your point. The C programmer will think of ^x (or alternatively x[0]) as a handle on the string "foo", the same way a good Java programmer would. Some programmers (Java or otherwise) might confuse a handle on "foo" with "foo" itself, but they'll be bitten by that when they try to write a swap function or mutate a string literal or whatever. Java's reference types are restricted pointers that you can only dereference using . or array indexing, you can't use array indexing on any arbitrary reference (it has to be of the right type, but C pointers were already strongly typed), you can only do arithmetic on them via indexing, and you're not allowed to refer to the non-pointer type. None of those differences make them not pointers, to me.
- sesteel 9y agoI'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 9y 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 9y agoright, so the object is passed by reference, and the object reference is passed by value...