Power to Build

Home » Programming » Java » Java: Pass by what?

Java: Pass by what?

I was talking to a co-developer last week during one of our lunch time walks(yes, we talk tech during out walks – even if our topic starts with something else, we always end up with tech stuff – once a geek, always a geek! :)). He is a C programmer and we often discuss Java vs C. One thing that came up last week was about Pointers. Having done both C and Java, I started explaining how Java doesn’t really have pointers, but internally it uses pointers. And that though Java has pointers (actually references), we won’t be able to change the address of a pointer like you would in C or C++.

In this context, we touched on parameter passing. I mentioned casually that Java passes the primitives by value and objects by reference (this seems to be common urban myth! Judging by the hits on this topic in Google, I think I am not the only one thinking this). I thought this would make sense, as it would be ridiculous for Java to pass around bulky objects, but instead the pointers (references) to the objects around. This makes sense, as you are able to change the values of attributes inside the object. (This was only possible in C/C++, if you passed the pointer/reference/address to an object/structure). Since we are passing around reference to the object, I said, it’s pass-by-reference when it came to objects, with the condition that “you cannot change those references”. This is where I slipped.

Apparently, those references (addresses) of those objects are passed around “by value” – meaning you cannot change them. And thus the argument that everything is pass-by-value in Java. Definitely got me. As the O’Reilly book describes it, “Java handles Objects and Arrays “by reference”, but when it comes to parameter passing, it is a Pass-by-value language. When a reference type is involved, the value that is passed is the reference!!”. Here is a nice discussion on Stackoverflow about this. There are several nice responses there that explain this in detail, so I won’t waste your time explaining the same thing here.

But Java is not alone in this – here is a nice discussion on the topic in Python. In the author’s words,

Unfortunately, Python is “pass-by-object-reference”, of which it is often said:

“Object references are passed by value.”

Incidentally, C is also a pass-by-value by default, but a programmer can achieve pass-by-reference there by passing pointers or pointer-to-pointers!! See here. It’s splitting hair at this point, but many languages are pass-by-value by default, but offer some mechanism to pass-by-reference. Even PHP employs a similar technique to pass by reference, as discussed here. Great wikipedia offers this on the topic:

Many languages support call-by-reference in some form or another, but comparatively few use it as a default, e.g. Perl. A few languages, such as C++, PHP, Visual Basic .NET, C# and REALbasic, default to call-by-value, but offer special syntax for call-by-reference parameters. C++ additionally offers call-by-reference-to-const.

There we go, the saving grace. At least, my understanding was not totally off, though I needed new terminology!! Moral of the story is “never assume anything” in IT.

Here are some more interesting links on the topic:

http://javadude.com/articles/passbyvalue.htm

http://programmers.stackexchange.com/questions/141834/how-is-a-java-reference-different-from-a-c-pointer

http://docstore.mik.ua/orelly/java-ent/jnut/ch02_10.htm

http://programmers.stackexchange.com/questions/153474/why-are-so-many-languages-passed-by-value

http://www-cs-students.stanford.edu/~sjac/c-to-cpp-info/references

http://robertheaton.com/2014/02/09/pythons-pass-by-object-reference-as-explained-by-philip-k-dick/


Comments, please?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: