Nagaraju Mulinti wrote:I think java only support pass by reference only, not pass by value.
No, exactly the contrary - Java always passes arguments by value. Java does not support pass-by-reference.
For primitive values, this is easy to understand - when you pass a primitive value to a method, it just passes the value, and not a reference to the variable that holds the value.
Non-primitive values are references to objects. The thing that makes it confusing is that you have to understand that those references are passed by value. In other words, the reference to the object is passed by value - that's not the same as pass-by-reference (which implies that there's a reference passed to the variable itself).
I too used to get so confused on this topic. The *only* thing to keep in mind in this topic is when OBJECTS are passed (as opposed to primitives like int), we are *not* passing the object BUT a reference (or pointer if you will) and we pass this reference using pass by value. So now we have *TWO* copies of references, BUT only *ONE* single copy of the object. This makes sense, because objects are made using "new" operator, and since we have created the object just once we should have just one copy of the object.
The called method can now use *its* copy of the reference (which was passed by value) to alter the values of this object, and THIS phenomenon we confusingly perceive as passing by reference.