We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Strings interact with each other: string = string2
Page Index Toggle Pages: 1
Strings interact with each other: string = string2 (Read 781 times)
Strings interact with each other: string = string2
Dec 19th, 2009, 2:50am
 
Hey everybody!
I have a little problem. If I write stringArray1 = stringArray2 and then perform changes on stringArray1, they also affect stringArray2.

I have this example sketch:
Code:

void setup() {
  String[] string1 = {"Hey, it's xmas soon", "I can't wait"};
  String[] string2 = string1;
//Now string2 should be same as string1
  string2[0] = string2[0].substring(2);
//Performing changes on string2, this should not affect string1
  println(string1);
//Output: [0] "y, it's xmas soon" [1] "I can't wait"
}

void draw() {
}


I hope you get my point. I already tried making string1 final, but that didn't help either. What can I do to leave string1 unaffected, when I take changes on string2?

Thanks for your help in advance,
processingLover  Smiley
Re: Strings interact with each other: string = string2
Reply #1 - Dec 19th, 2009, 3:30am
 
i actually dont know why that happens, but use arrayCopy() for copying arrays. Like this

Code:
 
String[] string1 = {"Hey, it's xmas soon", "I can't wait"};
String[] string2 = new String[string1.length];

arraycopy(string1,string2 );

string2[0] = string2[0].substring(2);

println(string1);
println(string2);
Re: Strings interact with each other: string = string2
Reply #2 - Dec 19th, 2009, 4:00am
 
It has a lot to do with what is explained here: http://processing.org/discourse/yabb2/num_1261136027.html

When assigning one array to another your are actually copying the reference of the array. The same is true for objects.
Some more code to demonstrate:

Code:

void setup()
{
 TestClass t1 = new TestClass(1);
 println(t1.n);
 TestClass t2 = t1;
 t2.n = 2;
 println(t1.n);
 println(t2.n);
}

class TestClass
{
 int n;
 
 TestClass(int n)
 {
   this.n = n;
 }
}
Re: Strings interact with each other: string = string2
Reply #3 - Dec 19th, 2009, 4:04am
 
This first line

String[] string1 = {"Hey, it's xmas soon", "I can't wait"};

creates a String array of length 2. It then sets the value of string1 to be a reference to this newly created array. Note that the value of string1 isn't quite the array. Rather, the value of string1 points to the array.  

When you say

String[] string2 = string1;

you are also telling string2 to point to the same array as string1 (i.e., you never actually created another array, you just created another variable that refers to the same array).

edit: whoops, JR beat me to it

Re: Strings interact with each other: string = string2
Reply #4 - Dec 19th, 2009, 4:08am
 
Thank you! arraycopy() works. Smiley

@d.rifkin and @JR I'm not sure what the advantage of using references is, though. And I never programmed in C, so I don't understand what you're doing in that topic  Shocked
Re: Strings interact with each other: string = string2
Reply #5 - Dec 19th, 2009, 4:33am
 
Look at it like this: a reference points to a value and a value .. well that's the value itself.

If you copy the value (for primitive data types):
int i1 = 0;
int i2 = i1;
and you change i2, i1 will not the be changed... because you copied the contents of i1 into i2.

If you copy a reference (which is what happens when you assign objects and arrays):
SomeObject o1 = new SomeOjbect();
SomeObject o2 = o1;
o1 and o2 will point to the same values. So if you change one, the other will change too.
Re: Strings interact with each other: string = string2
Reply #6 - Dec 19th, 2009, 4:37am
 
processingLover wrote on Dec 19th, 2009, 4:08am:
@d.rifkin and @JR I'm not sure what the advantage of using references is, though.


There are many advantages of using references over values.
But one of the more important ones is assigning objects and arrays to each other.
Suppose you have a very, very large array. If you are going to copy the values in the array to another when assigning, this will make your program slooooow. If you only copy the reference, it will not.

You can still decide to copy the values by using arrayCopy, but then you know what is happening behind the scene. And you are responsible for the (possible) slowness of your program/sketch.

The same is true for objects.
Page Index Toggle Pages: 1