This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
which of following correctly describes how variablers are passed to methods?
a)Arguments are always passed by value b)Arguments are always passed by reference c)Arguments that are primitive types are passed by value d)Arguments that are passed with & operator are passed by reference
I've a doubt here. When we pass any object references, a copy of that is passed to the function and I think thats the reason why we cannot change the reference itself but are allowed to change the object state that it is referring. In that case don't you think "option a" is correct.
When passing objects the reference value is passed rather than the entire object. However it can be argued that you are still passing a value ( which contains that reference ). The SCJP book written by Kathy and Bert does say that java is pass by value for all variables. From the exam's point of view i guess you could say a is also right. Thanks for pointing that out. If someone thinks the answer could be otherwise please feel free to correct me.
Reference page 204: SCJP 1.5 by Kathy and Bert, line 3 under the heading "Does java use pass by value-semantics" [ April 16, 2006: Message edited by: John Meyers ]
Don't get confused about changes in the object state thinking it's passed as reference.
Everything in Java is passed by value, even object references.
For example, even though you can change a state of an object passed as reference to a given method, such reference is still passed by value. So the correct answers are A and C.
Let me explain by using code :
Output is : 999 2 3 4 5 6
The key point here is that even though you can change the array elements or any object reference's state, you can NEVER change the original variable passed to the method. You can NEVER change the memory place where the original reference is "pointing to" from within the method. So the argument is passed ALWAYS by value. [ April 16, 2006: Message edited by: Edisandro Bessa ]
"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
The key is to recognize that object variables are just references to an object and not the object itself.
Then, object reference variables are passed by value just like any other variable.
Let's say we have these methods:
When String person = "Elvis" and saySomething(person) are executed:
We can conclude that person and someone are two different reference variables poiting to the same object.
Then after String someone = "Hello "+someone
Altering the variable reference of someone will not alter the reference value of hello. Hence we can conclude that references are passed by value, altering the reference of an argument will not alter the reference of the actual parameter.
I hope this helps. [ April 16, 2006: Message edited by: Edwin Dalorzo ]