File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Strings as function arguments" Watch "Strings as function arguments" New topic

Strings as function arguments

kanna kesavan

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

Joined: Apr 24, 2004
Posts: 23
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.

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
Karan Rajan

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:
subject: Strings as function arguments
It's not a secret anymore!