Java: Pass by value or Pass by reference

Java: Pass by value or Pass by reference?
Short: Pass by value

This is basically for memory optimization, as I would like to think. Primitive data types have specific length whereas objects do not. In the memory both of these are stored in a continuous sequence of bits. The primitive types are stored from, if you will, low number to high number whereas objects are stored from higher number to lower in order to find the continuous free blocks.

It is generally agreed that primitives are passed by value and objects (memory address) are referenced. However, even the object references are passed by value.

In C# you can actually pass by reference and the original object also gets modified.
E.g
void passMe(ref OrginialObj obj)
{
obj = ....;//original object is modified even after the method returns
}

 

Comment on this:

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