• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assesment test question

 
Lavanya Raguram
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Pls explain
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c is correct. there is no &variable name in java to refer to the address of a variable. Object types are passed by reference and primitives are passed by value which makes a and b incorrect.
 
Asha Pathik
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John,

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.

Asha
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

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 ]
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic