Nice little article here.
pratapsiva sivakumar wrote:Hi why is java strictly called "call by value" and not "call by reference" .Please Explain with an example?
Also Search First. You will surely get valuable replies as this question already been asked in the forum.
1. primitive data types are passed by value
2. objects are passed by reference (since when you modify the object the original object is what gets modified)
But, the second statement is not strictly true.
When you pass objects to methods, what you are actually passing is reference(pointer) to the object (and not the object itself).
In java this reference is passed 'by value', in other words the reference/pointer is copied and passed (just like a value).
So the method receives an object handle; by value.
Therefore, in java method parameters are passed 'by value' both primitives and objects.
Maybe this article gives some hint http://javadude.com/articles/passbyvalue.htm
This is a superb article. Good one.
Jothi Shankar Kumar wrote:It explains what is Pass By Value but does not answer the original question of why should java use pass by value?
In Java, variables of non-primitive types are references to objects. This is different from C++, where such a variable is the object itself. There are no variables that directly represent objects in Java.
Java has inherited a lot of its ideas from C++, but the designers of the Java programming language wanted to leave the complicated stuff from C++ out. Passing by reference was probably one such thing, and since non-primitive type variable are references already, there was really no need to have both pass-by-value and pass-by-reference in Java - that would make the language more complicated with little benefit.
Note that in Java, references are passed by value - that's something different than passing by reference.