| Author |
Strings as function arguments
|
kanna kesavan
Greenhorn
Joined: May 09, 2005
Posts: 6
|
|
|
When strings are passed as arguments to functions,is it pass by value or pass by reference?
|
 |
Timmy Marks
Ranch Hand
Joined: Dec 01, 2003
Posts: 226
|
|
According to this,
Java never uses call by reference. It always uses call by value.
but this link says that:
The objects, i.e. all the classes which derive from the base Object class, are passed as references in functions.
I tend to agree with the javaranch one (the second link). A method will get a reference to the String, and since Strings are immutable, you cannot change the value of the String in the caller.
|
 |
Krishnan Loganathan
Greenhorn
Joined: Apr 24, 2004
Posts: 23
|
|
Hi, In java, the primitive type variables are call-by-value and objects are call-by-reference. But for String, still its a call-by-reference, u cannt modify the value since it is immutable. Cheers, Loga
|
 |
Sripathi Krishnamurthy
Ranch Hand
Joined: Mar 07, 2005
Posts: 232
|
|
Java only fakes pass by reference. Java always uses pass by value. Java copies and passes the reference by value, not the object http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
|
 |
Karan Rajan
Greenhorn
Joined: May 19, 2005
Posts: 12
|
|
I agree with Sripathi. My two-cents worth : Java always passes by value ( or as the Sierra and Bates put it ... pass by copy of the variable ) for all variables. - in case of primitive you are passing a copy of the bits representing the value. - in case of object references you are passing a copy of the bits representing a specific object on the heap. ( thus "faking" the pass by reference).
|
 |
 |
|
|
subject: Strings as function arguments
|
|
|