String problem again!!

I have a string and I wanted to keep it's value evenwhen it passes from one method to another!!Is this possible in any way!?Ie acts like an object rather then like a primitive?
[198 byte] By [sim085a] at [2007-10-2 5:45:45]
# 1
> I have a string and I wanted to keep it's value> evenwhen it passes from one method to another!!Pretty easy. Strings never change their values anyway. They can't.> Ie acts like an object rather then like a primitive?Strings are objects.
CeciNEstPasUnProgrammeura at 2007-7-16 1:55:36 > top of Java-index,Java Essentials,Java Programming...
# 2

> 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" );

}

mlka at 2007-7-16 1:55:36 > top of Java-index,Java Essentials,Java Programming...
# 3

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;

}

}

}

sim085a at 2007-7-16 1:55:36 > top of Java-index,Java Essentials,Java Programming...
# 4
Wrap it (1), or use an mutable CharSequence, like StringBuilder(2).Thanks ... as you see in the example I was going to use a wraper .. but now I am gona use a StringBuilder :) or try to :)
sim085a at 2007-7-16 1:55:36 > top of Java-index,Java Essentials,Java Programming...
# 5
> 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?
sim085a at 2007-7-16 1:55:36 > top of Java-index,Java Essentials,Java Programming...
# 6

> > 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.

DrClapa at 2007-7-16 1:55:36 > top of Java-index,Java Essentials,Java Programming...
# 7

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.

Dick_Adamsa at 2007-7-16 1:55:36 > top of Java-index,Java Essentials,Java Programming...
# 8

> 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]

mlka at 2007-7-16 1:55:36 > top of Java-index,Java Essentials,Java Programming...
# 9

> 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(); }

sim085a at 2007-7-16 1:55:36 > top of Java-index,Java Essentials,Java Programming...
# 10

> 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?

CeciNEstPasUnProgrammeura at 2007-7-16 1:55:36 > top of Java-index,Java Essentials,Java Programming...
# 11
s/body/buddy.
CeciNEstPasUnProgrammeura at 2007-7-16 1:55:36 > top of Java-index,Java Essentials,Java Programming...
# 12

> 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]

mlka at 2007-7-16 1:55:36 > top of Java-index,Java Essentials,Java Programming...
# 13

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?

sim085a at 2007-7-16 1:55:36 > top of Java-index,Java Essentials,Java Programming...
# 14
> Cup Size -- a story about variablesI am reading it right now.
sim085a at 2007-7-16 1:55:36 > top of Java-index,Java Essentials,Java Programming...
# 15
> So do you really expect that magically, your finger is pointing > to McD's now, even though you didn't move at all?Thanks, that really help out to make me understand :)
sim085a at 2007-7-20 18:41:57 > top of Java-index,Java Essentials,Java Programming...
# 16

> 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

kajbja at 2007-7-20 18:41:57 > top of Java-index,Java Essentials,Java Programming...
# 17
yee...I am still confused on the basics .. which I am afraid it is not so good, because I will keep coming with stupid questions. I will do some searching and find some articles on the net.
sim085a at 2007-7-20 18:41:57 > top of Java-index,Java Essentials,Java Programming...