1

Why does this work, displaying array in sorted order?:

Integer[] array={7,5,9,3,6,0,2,4};

MergeSort.mergeSort(array,0,7);

System.out.println(Arrays.toString(array));

Specifically, why does passing array to a public static void method mergeSort end up modifying the array itself? I thought that Java protected from this. For example this code:

  public static void main(String[] args){
    int c=2;
    change(c);
    System.out.print(c);
  }
  public static void change(int c){
    c=4;
  }

returns 2 instead of 4. I am confused why Java allows you to modify an array passed as a parameter, but not an int

2 Answers 2

4

Because Java is pass by value, and when you pass an Object (and array is an Object) you pass the value of the reference to the Object. In contrast, a primitive has no reference so you just pass the value.

Edit

Similarly, you could have used the built in Arrays.sort() method to sort your array. Java arrays are not primitive types. In Java, the primitive wrapper types are immutable (like String). Consider,

private static void change(String in) {
    in = in + " World"; // <-- modifies in here, can't change caller's reference.
}

private static void change(StringBuilder in) {
    in.append(" World"); // <-- uses reference from caller.
}

public static void main(String[] args) {
    String str = "Hello";
    StringBuilder sb = new StringBuilder(str);
    change(str);
    change(sb);
    System.out.println("one: " + str);
    System.out.println("two: " + sb);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Is this answer really complete ? The erroneous answer of hexafraction shows that there is a few more to say about this, because just replacing c by an array would not change anything
@Dici why Java allows you to modify an array passed as a parameter, but not an int? Because an int is a primitive. And the wrapper types are immutable.
Yep, but not as hexafraction suggests it. If it is an object, a side-effects method must be called on it, if it is an array, we have to directly assign a value to a[i]. myParameter = someNewValue; has no effect, I think it is important to explain here.
0

When using an array, taken as a parameter or not, consider that arr[i] is a pointer to some location of the memory. Then, when receiving an array as a parameter, you have a new reference pointing to the same array object. That means that in your method, instructions such as :

myParameterArray = new int[5];

won't modify the input array, because you are just changing the value of a reference, not the value pointed by it. Nevertheless, instructions such as :

myParameterArray[i] = 5;

will directly modify your input array. That is how methods like Arrays.sort work.

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.