Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing strings to methods

 
Chris Meijers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When passing a string to a method, and that string is 'changed' by calling 'append' on it, what happens to the original string?:
public void change(String s) {
s.append("a");
//s now points to new String instance.
}
What happens when I do
String name="Chris";
append(name);
System.out.prinln(name);
I thought name now contains "Chrisa". Am I correct?
 
Oliver Grass
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chris,
i'm not sure, what you really want to do with your code...
Do you want to show, that you can append something to a String-object???

First: There is no append-method on String.
Second: String can never be changed, they are immutable.
Perhaps the Code below helps to understand....

Hope that helps,
cheers
Oliver
 
Chris Meijers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes you are right. I mixed up all kinds of things. What I was trying to say is this:
Class X{
public static void change(String s)
{
s=s+"a";
}
public static void main()
{
String name="Chris";
change(name);
System.out.prinln(name);
}
}
usually, when an object is changed in a method(so not the reference but the object itself) this change is reflected on the caller. But a string is not an ordinary object in the sense that any 'change' implies a change of reference. So after using +"a" the string referres to a different object. This would indicate that after the method returns, the caller still referrs to the original string ("Chris"). So in effect, even though String is an object, it would exhibit primitive-like behaviour when used as a parameter in a method call.
So what is the printed result after calling 'change'. "Chris" or "Chrisa"?

 
Aru Ven
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris,
A copy of the string s is passed to the method. NOT THE ORIGINAL STRING. So in the method change, s = s + a creates a new String "Chrisa". And when u print the string s after calling the method in the main it is still the string with original value "Chris", bcoz a copy of the string was passed to the method.
HTH,
ARuna
[This message has been edited by Aru Ven (edited November 08, 2000).]
 
Oliver Grass
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Originally posted by Chris Meijers:

original string ("Chris"). So in effect, even though String is an object, it would exhibit primitive-like behaviour when used as a parameter in a method call.
So what is the printed result after calling 'change'. "Chris" or "Chrisa"?


The printed result is "Chris"...
The contents of a String NEVER change, cause they are read-only. So when you pass "Chris" to the method change, you will get a complete new Object and the original one will be unchanged. You are right with your statement, when you are saying, that String behaves like a primitive-type. In this case its true....
That's my understanding, correct me if i'm wrong
cheers
Oliver
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic