6 ms·
> Java passes "references by value". Meaning the target method receives a _copy_ of the pointer, not the actual pointer. Which is exactly how C does it. I don'
by cbd1984 12y ago
> Java passes "references by value". Meaning the target method receives a _copy_ of the pointer, not the actual pointer.
Which is exactly how C does it. I don't see how this is a difficult enough concept to grasp that we need an article explicitly pointing out how Java is just like C in this respect.
- Dylan16807 12y agoBecause in C you have to specifically ask for something to be a pointer. "pass by reference" is an incorrect phrasing, but at least it's an attempt. There is a crucial distinction between Java and C. And C++ shows actual reference passing.
- cbd1984 12y ago> "pass by reference" is an incorrect phrasing No, it isn't. In C, a pointer is a reference. It supports arithmetic, but it works like a reference as well, so the name applies. What might confuse people is that in C, you do have to specifically ask for a reference to something, whereas in Java, everything which isn't a primitive type is already a reference. It's true your code can't dereference those references such that it gets access to all of the data in a List object, for example. C works the same way, again: It's entirely possible to say something like 'typedef struct foo mfoo;' and then declare all of your functions to take pointers to type mfoo, and client code will never be able to get its hands on a struct foo object. FILE pointers work just like this, for example.
- rifung 12y agoI don't think a pointer in C is really a reference though? If you pass a pointer to a function, then you are saying, hey allocate a new variable to put on the stack which is a pointer. If you pass a reference, then at the language level, you are saying hey I want that function to use THIS variable that I already have. While the implementation may certainly be free to use a pointer, they are not fundamentally the same concepts. Or so I think.
- DonHopkins 12y agoWhat something IS is independent of how you PASS it. An integer isn't a reference, but you can pass a reference to an integer in C++ (to implement swap, for example), while you can't do that in Java.
- nknighthb 12y agoA pointer in C is a reference (see ISO 9899:1999 § 6.2.5) under siege by nasal demons (see § 6.3.2.3). The implementation isn't really required to do many of the things people assume it is. (It's ironic, by the way, that in seeking to be more pedantic, you reference stacks. Stacks are not part of C.)
- jjnoakes 12y agoA C pointer is not a reference in the way we are using the word reference in this discussion (as in pass-by-reference, as in an alias for another value without applying any operators). Sure, the C standard uses the English word "reference", as in "refers to" and "referred to", but letting that confuse you without understanding the meaning of the word in context is what lead to this Java mess in the first place. Don't bring C into it too.
- nknighthb 12y agoI'm not confused at all, thank you. I'm of the long-standing and well-informed opinion that "pass-by-reference" and "pass-by-value" are worthless terms that need to die, and that this entire conversation is an exercise in useless academic pedantry.
- jjnoakes 12y agoBased on your previous reply, you are indeed confused. Reference is a long-established term, and using it to refer to C pointers is typically considered incorrect. In addition, pass-by-reference and pass-by-value are very useful terms for anyone interested in accurately speaking about and understanding what will happen when they run the program they write. There are people who consider accuracy and understanding to be pedantry. More often than not, in my experience, they are a larger source of bugs than those who strive to truly understand how their tools work.
- learc83 12y agoPass by reference has a specific meaning and specific consequences. In C to be able to simulate some aspects of pas-by-reference you need a double pointer.
- slowmovintarget 12y agoWell, he also has articles on tips for using Visual Age for Java, and a copyright notice starting in 1996, so perhaps this is yet another blast from the past that made it to HN's front page. Because that particular argument used to be a thing.
- jacquesm 12y agoLast-Modified: Thu, 05 Mar 2015 18:22:25 GMT
- slowmovintarget 12y agoFirst version of the article is at least from 2002: http://web.archive.org/web/20020109181501/http://javadude.com/articles/passbyvalue.htm http://web.archive.org/web/20020109181501/http://javadude.co... Good that it's kept crisp.
- have_humility 12y agoUtterly unreliable for determining the age of an article.
- learc83 12y agoNot everyone knows C. A large percentage (maybe even the majority) of programmers who graduate from Java schools don't know C very well at all. For them, "it works just like C" is an insufficient explanation.
- tedunangst 12y ago"It works just like Python"
- rifung 12y agoI don't know if that'd help or hurt, I've definitely seen people also say Python is pass by reference. It seems like they have the same problem.
- _ZeD_ 12y agoStorically, python devs says the python language use "pass-by-name"
- icebraining 12y agoHow so? Reading [1] on pass-by-name, I don't see how it applies to Python, which evaluates the function arguments before passing them. The example given would be, in Python, def double(x): x *= 2 a = [4] double(a[0]) If Python was pass-by-name, a would now be [8], but it's actually still [4]. [1] http://www.cs.sfu.ca/~cameron/Teaching/383/PassByName.html http://www.cs.sfu.ca/~cameron/Teaching/383/PassByName.html
- ptx 12y agoExactly. Objects in Java work just like objects in Python: pointers passed by value. The difference is that everything in Python is an object, whereas Java additionally has primitive types where the value itself is passed by value.
- userbinator 12y agoOr Asm, for that matter (likely even fewer programmers); at that level, basic concepts like memory addressing and pointers become almost "obvious facts of life". As someone who started in Asm, I think pass-by-value/pass-by-reference are also intuitive there in the true "understood without needing any explanation" sense, since basically everything you'd pass to a procedure is a value of some sort, and it only depends on whether you are to interpret that value as a memory address.
- kelnos 12y agoI think the confusion comes from one main place. C has the address-of operator (&). So you can pass the address of a pointer, change what that points to in a function, and then the variable outside the function has indeed changed to point to a new chunk of memory. Java only has primitives and pointers to objects, and it only allows you to pass those by value to functions. You can't take the address of a primitive or object and pass that. So basically in C you can pass an object (struct) or even primitive by reference... or at least you can fake it with trivial syntax. In Java you just cannot do that, and I think Java's use of the term "reference" to mean "pointer" makes some people (understandably) confused.
- lucio 12y ago>You can't take the address of a primitive or object and pass that. Not correct. In Java, if you have an object you take the address and pass it. If you have a primitive, you pass a copy of the value. Both things are called "pass-by-value" (something that confuses a lot of people).
- jjnoakes 12y agoJava has no notion of "taking the address of an object" because you never have an expression that refers to an object directly. You only have the addresses, so there's no need (or way) to convert objects to their address. And it's all pass by value (for all values you could have - numbers, addresses, booleans, whatever).
- kelnos 12y agoHeh, see why this is so confusing? That is of course what I meant -- it's really that Java, passing-wise, just has primitives and addresses to objects.
- mannykannot 12y agoThe number of people who don't get it, or don't realize it is different from actual pass-by-reference and that the difference has actual consequences, is reason enough for an article. In my experience, imprecise semantics and the misunderstandings that arise from them are a/the major cause of error, in both the requirements and implementation domains.