I do know that when you pass an array as a parameter into a method and then alter it in some way, the original array will be changed. However, does this also apply to Strings?
If you have these variables:
int[] testArray = {1,2,3};
String testString = "helloWorld";
And these methods:
static int[] changeFirstEntryToZero(int[] array) {
array[0] = 0;
return array;
}
static String changeString(String s) {
s = "changed";
return s;
}
And then in the main method you do this:
System.out.println(testArray[0]);
changeFirstEntryToZero(testArray);
System.out.println(testArray[0]);
System.out.println(testString);
changeString(testString);
System.out.println(testString);
Then the result is this:
1
0
helloworld
helloworld
That tells me that it does not permanently change the String. But why?
I thought that Strings are objects too in Java. Is there something I'm missing, or is it that simple?
Could a String accidentally be permanently changed if I did something slightly different.