Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strings as function arguments

 
kanna kesavan
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When strings are passed as arguments to functions,is it pass by value or pass by reference?
 
Timmy Marks
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 232
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic