> Ie acts like an object rather then like a primitive?
It does act like an object, it is just that object is imutable.
Wrap it (1), or use an mutable CharSequence, like StringBuilder(2).
1)
class MeStringWrapper {
public String bob;
}
public void changeString( MeStringWrapper bob ) {
bob.bob = bob.bob + "bob";
}
2)
public void changeString( StringBuilder builder ) {
builder.append( "bob" );
}
Not exactly ...
In the example below the value of a dosent change.
public class Test{
public Test(){
String a = "Hello, World [1]";
String b = updateHelloWorld(a);
// Value of 'a' did not change.
System.out.println(a);
System.out.println(b);
}
public String updateHelloWorld(String a){
a = "Hello, World [2]";
return a;
}
public static void main(String[] args){
new Test();
}
class StringContainer{
String aString;
}
}
While in the following example the value a changes because the string is raped in another object!
public class Test{
public Test(){
StringContainer a = new StringContainer("Hello, World [2]");
StringContainer b = updateHelloWorld(a);
// Value of 'a' does change!!!.
System.out.println(a.aString);
System.out.println(b.aString);
}
public StringContainer updateHelloWorld(StringContainer a){
a.aString = "Hello, World [2]";
return a;
}
public static void main(String[] args){
new Test();
}
class StringContainer{
public String aString;
public StringContainer(String aString){
this.aString = aString;
}
}
}
> > It does act like an object, it is just that object
> is imutable.
>
> btw - is this one of the special favors doen by the
> compiler?
No, not the compiler. It just doesn't have any public methods that allow it to be changed, that's what makes it immutable.
You're probably confusing the String object with the variable that holds a reference to the object. Yes, the reference held by the variable can certainly change (that's why it's called a "variable"). One approach would be to declare the variable final, so it can't change.
> Not exactly ...
What is "not exactly" repling too?
> In the example below the value of a dosent change.
Noi, the value does not change, you change the refrence.
> > public String updateHelloWorld(String a){
> a = "Hello, World [2]";
> return a;
> }
>
Do you expect this to work:
Object o = new Object();
fiddle(o);
public void fiddle( Object o) {
o = new Object(); }
As that is what your string is doing.
[url=http://www.javaranch.com/campfire/StoryCups.jsp]Cup Size -- a story about variables[/code] - [url=http://www.javaranch.com/campfire/StoryPassBy.jsp]Pass-by-Value Please (Cup Size continued)[/url]
> Do you expect this to work:
Yes, in that case, as my logic goes, 'o' will be pointing to a new instance both inside the method and outside it.
Object o = new Object();
fiddle(o);
// Here o sould display the value set
// to it inside the method fiddle!
System.out.println(o.toString());
public void fiddle( Object o) {
o = new Object(); }
> Yes, in that case, as my logic goes, 'o' will be
> pointing to a new instance both inside the method and
> outside it.
You and your body stand on some plaza, wondering where to go. You point to your favorite pub. Your buddy gets the suggestion (method argument) and points to it, too, while forming his opinion about it. Doing that, he realizes that he's rather have something to eat and turns to point to McD's (new Object in method).
So do you really expect that magically, your finger is pointing to McD's now, even though you didn't move at all?
> Yes, in that case, as my logic goes, 'o' will be
> pointing to a new instance both inside the method and
> outside it.
Nope, other people can explain it better, read:
[url=http://www.javaranch.com/campfire/StoryCups.jsp]Cup Size -- a story about variables[/url]
[url=http://www.javaranch.com/campfire/StoryPassBy.jsp]Pass-by-Value Please (Cup Size continued)[/url]
Ok .. I understand (takes me back to when we had that argument with by value and by reference).
So if I understand your points well... Java always pass by value, that is when I pass 'o' as an argument I am really passing the address of 'o', however when I am doing o = new Object(); I am giving a new adress to that 'o' object within that method, but that address is only for the 'o' within that method and not for the 'o' which address has been passed as an argument to that method.
correct?
> So if I understand your points well... Java always
> pass by value, that is when I pass 'o' as an argument
> I am really passing the address of 'o', however when
> I am doing o = new Object(); I am giving a new adress
> to that 'o' object within that method, but that
> address is only for the 'o' within that method and
> not for the 'o' which address has been passed as an
> argument to that method.
>
> correct?
You are passing a copy of the o reference to the method. O will still point to the original object when you return from the method (since you only had a copy of the reference)
Kaj