This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Strings as function arguments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings as function arguments" Watch "Strings as function arguments" New topic
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings as function arguments
 
Similar Threads
Passing String array as argument to main() method
Using commandline arguments Query
ejbActivate() and ejbPassivate()
Error with this array?
How to pass arguments from one program t o other