Monday, December 19, 2011

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
}


 

No comments:

Post a Comment