17 ms·
Well the way I've always thought of it is that Java passes "references by value". Meaning the target method receives a _copy_ of the pointer, not the actual poi
by nickbauman 12y ago
Well the way I've always thought of it is that Java passes "references by value". Meaning the target method receives a _copy_ of the pointer, not the actual pointer. So the result of newing up the parameter inside the target method doesn't affect the original, because you're just making the pointer point to a NEW memory address. On the other hand, mutating the parameter WILL affect the original object, because a copy of a pointer still points to that object from the caller.
- 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.
- 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.
- TazeTSchnitzel 12y agoWhat Java does is pretty much equivalent to putting an asterisk in your C/C++ typedefs: typedef struct _foo* Foo;
- metaobject 12y agoI'm guessing this is why you have to call "new" to create new objects in Java?
- TazeTSchnitzel 12y agoJava's new is similar to C++'s new as I understand it, yes. It allocates memory, initialises an object, and produces a pointer.
- nbouscal 12y agoThe article says almost exactly that: "However, Objects are not passed by reference. A correct statement would be Object references are passed by value."
- revelation 12y agoYeah, it says that in one sentence while its the central point of confusion if you even want to call it that. Instead it rants at length to somehow save definitions that are clearly not particularly helpful in a language like Java. This article just reeks of this certain kind of socially inept, completely unhelpful attitude and technical pedantry that has infected mailinglists and IRC channels over the world. Actively trying to misunderstand people in an effort to expose their ignorance or unfamiliarity with intricacies of whatever language or tool they are trying to navigate. Its a terrible habit up there with feigned surprise.
- inflagranti 12y agoThis is a very unjustified and overly harsh criticism. The article clearly presents real world confusion that can arise from not understanding that Java is not pass by reference. This can easily lead to wrong code. So it's not just pedantically insisting on some specific terminology, but highlighting the semantically differences between passing a reference (more precisely pointer) by value vs. 'real' pass by reference.
- olavk 12y agoI've never seen actual bugs due to a misunderstanding of the concept though. It seem to be mostly a terminology difference between c++ and Java cultures. It is correct to say that Java passes objects as references, but it is technically wrong to say that Java is pass-by-reference. But since Java has only one way of passing objects, this terminology confusion does not lead to actual problems.
- jjnoakes 12y agoExcept they are different things, and others (me included) have seen bugs related to the confusion. Technical correctness is always best. Java is pass by value, and even variables holding object references are passed by value.