To all,
I was working with a String parameter which would be altered during
various method calls; an example code as belows:
public class BlockMatchSimulation
{
class InnerBlock
{
public String mName;
}
public void replaceString (String pName, String pDelim, String
pNewValue)
{
pName=pName.replaceAll (pDelim, pNewValue);
}
public void appendString (String pName, String pSurName)
{
pName=pName+" "+pSurName;
}
public void replaceString (InnerBlock pObj, String pDelim, String
pNewValue)
{
pObj.mName=pObj.mName.replaceAll (pDelim, pNewValue);
}
public void appendString (InnerBlock pObj, String pSurName)
{
pObj.mName=pObj.mName+" "+pSurName;
}
public static void main (String[] pArgs)
{
String aName="Joseph";
BlockMatchSimulation aObj=new BlockMatchSimulation ();
BlockMatchSimulation.InnerBlock aIObj=aObj.new InnerBlock ();
aIObj.mName=aName;
// direct string handling
aObj.replaceString (aName, "ep", "");
aObj.appendString (aName, "House");
System.out.println ("Name is : "+aName);
// object level handling
aObj.replaceString (aIObj, "ep", "");
aObj.appendString (aIObj, "House");
System.out.println ("Name in the Object : "+aIObj.mName);
}
}
the output is :
Name is : Joseph
Name in the Object : Josh House
I was wondering why if I use the String parameter directly and called
the replaceString and appendString --> the updated value is not
returned!
On the other hand, it works perfectly when I encapsulate the String
field within an object.... why is it so?
As I remember String in java is handled by the JVM in a different way
(ie. String/literal pooling); is this the reason for such strange
behaviour?
>From Jason (Kusanagihk)
Thomas Weidenfeller - 18 Aug 2006 11:15 GMT
> I was working with a String parameter which would be altered during
> various method calls;
You can't. You can't alter a String in java once you have created one.
Period. You can replace a string reference with a reference to a new
string, and that is what code like
s = s + "X";
does. But it does not alter the original String object. After the above
line is executed 's' no longer references the original String object,
but a new one. That, plus Java's way of how arguments are handled in
method calls (object references are passed as values) ensures your
methods replaceString() and appendString() will never work the way you
have written them.
Get a good textbook about Java to learn the details. Consider returning
the new String references as return values from the methods, and/or
consider using a StringBuilder or StringBuffer.
/Thomas
Oliver Wong - 18 Aug 2006 20:28 GMT
>> I was working with a String parameter which would be altered during
>> various method calls;
>
> You can't. You can't alter a String in java once you have created one.
> Period.
Well, you can, but it's probably "very bad" to do so.[*] It's much
better to follow Thomas' advice:
> You can replace a string reference with a reference to a new string, and
> that is what code like
[quoted text clipped - 4 lines]
> line is executed 's' no longer references the original String object, but
> a new one.
- Oliver
* Hint: reflection + the String class stores its data as a private array of
characters.
Dieter Lamberty - 18 Aug 2006 11:17 GMT
kusanagihk@gmail.com schrieb:
> To all,
>
> I was working with a String parameter which would be altered during
> various method calls; an example code as belows:
> ...
Hi
Strings in Java are not mutable. What you do is to create new Strings
but don't use them (method return = void).
To do what you want to do you should use a StringBuffer or StringBuilder
Object.
Hope that helps
Dieter
kusanagihk@gmail.com - 20 Aug 2006 13:23 GMT
Thanks to all
Yeah, I nearly forgot String(s) are immutable... that's why it doesn't
work in this way.
And I've replace the String part with a StringBuffer instead, it works
as expected,
Thanks again
>From Jason (Kusanagihk)