It's actually by value...
Wednesday, December 22, 2004
I found this blog entry today: Why Java is better than C
- Which points to this article: http://www.jazillian.com/reasons.html
The article has good points, however, the following statement has something wrong with it:
Java has no "varargs"
There's no need for this "varargs" stuff. If you have a set of fields that you tend to pass around together, put them together into a class and pass an instance of the class around. If you have a function that works on a list of objects (and the list can be any length) then just pass a List object.
This very topic comes into discussions among Java developers of all sizes and colors. It is also a minor detail that gets overlooked and it wasn't very clear to me the first time I really thought about it: Java doesn't pass instances of objects around, as the article claims.
It's the old question of "by value or by reference?" It gets a bit tricky, as it's very intuitive to say "by reference."
However, it is not 100% accurate (A nice way to say wrong): Java passes parameters by value ONLY, but, it's the value of the reference.
So there are no instance of objects being passed around, like it's suggested in the article. Nor, are references of the objects passed around - It is actually the value of the "pointer" pointing to the reference - I know, I know...Java doesn't have any pointers...But, it does...A pointer is a pointer, regardless of how much you abstract its meaning and implementation...
So, if anyone asks if Java's parameters are passed by value or by reference, the correct answer is: by value.
It is kind of a trick question, me thinks - The trick is to explain why it is by value.
had an article/Q&A about this particular topic (A long time ago). If you are still not convinced, try a couple of detailed examples.