10

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

1
  • 2
    Arrays in Java are mutable, it not depends on array type, they will be mutable in any case. Commented Apr 20, 2013 at 22:00

4 Answers 4

20

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

Sign up to request clarification or add additional context in comments.

Comments

5

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.

Comments

2

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.