aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes passing strings to methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "passing strings to methods" Watch "passing strings to methods" New topic
Author

passing strings to methods

Chris Meijers
Greenhorn

Joined: Nov 07, 2000
Posts: 9
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

Joined: Nov 02, 2000
Posts: 65
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

Joined: Nov 07, 2000
Posts: 9
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

Joined: Sep 28, 2000
Posts: 199
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

Joined: Nov 02, 2000
Posts: 65
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing strings to methods