Reputation: 1270
I wonder if String arrays in Java are mutable ? I know that Strings are immutable, but how about string Arrays ?
If I have a string array, and change the content, will a new string object be created ? Or will the actual value just be changed ?
Thanks in advance
Upvotes: 10
Views: 35971
Reputation: 511
The String[]
array does not contain references to string variables, as some people might say. The String[]
array contains values (or more exactly references to values), but not references to variables.
public class Main {
public static void main(String[] args) {
String one="1";
String two="2";
String[] arr = {one, two};
System.out.println(arr[1]);
// Result is "2"
two="3";
System.out.println(arr[1]);
// Result is still "2"
}
}
So, as you can see, the arr[1]
is not a reference to String two
. It got the value from the variable two
and that's it. Changing the variable two
did not affect arr[1]
.
The same thing about ArrayList
:
//...
String one="1";
String two="2";
List arr2 = new ArrayList<String>();
arr2.add(one);
arr2.add(two);
System.out.println(arr2.get(0));
// Result is "1"
one = "one";
System.out.println(arr2.get(0));
// Result is still "1", not "one"
//...
Therefore the array String
elements are immutable (which is logical, because String
s are immutable).
The mutability occurs when you pass the arrays arr
or arr2
themselves to a procedure, not their immutable String
elements.
For example:
change(arr);
// where arr is a String[] array of {"1","2"}
// see the procedure below
System.out.println(arr[0]);
// Result is "one"
// ...
static void change(String[] someArray){
someArray[0]="one";
}
In other words, the array is passed by reference (=mutable), but its string elements are passed by value (immutable).
Upvotes: 2
Reputation: 85779
The String
s contained in the String[]
are indeed immutable, but the array is mutable.
This is well explained in this answer:
- Immutability means that objects of a certain type can not change in any meaningful way to outside observers
Integer
,String
, etc are immutable- Generally all value types should be
- Array objects are mutable
- It may be an array of references to immutable types, but the array itself is mutable
- Meaning you can set those references to anything you want
- Also true for array of primitives
- An immutable array will not be practical
- References to objects can be shared
- If the object is mutable, mutation will be seen through all these references
EDIT:
Somewhat related: Why can't strings be mutable in Java and .NET?
Upvotes: 20
Reputation: 677
In Arrays, each element is just a pointer to an object. So, when you do something like
String one = "1";
String two = "2";
String three = "3";
String four = "4";
String[] myStringArray = {one, two, three};
myStringArray[2] = four;
Then the pointer that was at the 3rd element of the array now points to the four instead of three.
Upvotes: 1
Reputation: 2644
As far as i remember the field in your array will reference another String
String[] array {"I","like","rain"};
array[2] = "sun"
your array can be changed. the Strings themselves not.
Upvotes: 5