7

Possible Duplicate:
Is Java pass-by-reference?

I am a little confused here. How does Arrays.sort(a) modify the value of a?

int[] a = {9,8,7,6,5,4,3,2,1};
Arrays.sort(a);
System.out.println(Arrays.toString(a));

I thought java was pass by value...

Community
  • 1
  • 1
Josh
  • 315
  • 1
  • 3
  • 8
  • Also, the jdk comes with the sources, so you can have a look at it if you want to learn more about it. – Puce May 25 '12 at 15:11
  • Just to expand on the answers: this only works because sort doesn't change the size of the array. If you wanted to do that, you'd have to allocate a new array and couldn't just modify the existing array. – biziclop May 25 '12 at 15:13

6 Answers6

11

Yes, Java is pass-by-value. However, what is getting passed by value here is the reference to the array, not the array itself.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 1
    So if you wan't to change the array in another method without changing the original one, you should make a copy of it. correct? – Josh May 25 '12 at 15:12
  • 1
    @Josh: Correct. You can use `a.clone()` to make a copy. – NPE May 25 '12 at 15:13
6

Objects in Java are passed by value of reference. So if you pass in an object, it gets a copy of the reference (if you assign that reference to something else, only the parameter is modified, the original object still exists and is referenced by the main program).

This link demonstrates a bit about passing by value of reference.

public void badSwap(Integer var1, Integer var2)
{
  Integer temp = var1;
  var1 = var2;
  var2 = temp;
}

Those are references to objects, but they will not be swapped since they are only the internal references in the function scope. However, if you do this:

var1.doubleValue();

It will use the reference to the original object.

Community
  • 1
  • 1
David B
  • 2,688
  • 18
  • 25
  • +1 It's important to remember: only the reference is "cloned", not the object referenced. We take this for granted in most cases but for some reason it causes confusion with arrays. – biziclop May 25 '12 at 15:14
  • Objects in Java are not passed by reference never. When you pass an object to a function, you pass the reference of the object (its value), e.g. in C++ you pass the reference and can do pointer arithmetic, in Java you can't do it. – Luiggi Mendoza May 25 '12 at 15:15
  • @LuiggiMendoza I like to think of it more as a "pseudo-reference". You can use it to access the object and its fields, but it's not comparable to a C-style pointer. – David B May 25 '12 at 15:17
  • You can take the concept as you like while you understand it, but just don't go there saying that Java pass by reference when it doesn't. @aix answer explains this in a simple way. – Luiggi Mendoza May 25 '12 at 15:19
  • @LuiggiMendoza I'm afraid I don't understand. I clearly state that it is passed by value of the reference. What part of my answer do you have an issue with? – David B May 25 '12 at 15:21
  • Sorry, you were right, I read "or" instead of "of" :( shame on me. – Luiggi Mendoza May 25 '12 at 15:24
  • @LuiggiMendoza No problem. It happens. :D – David B May 25 '12 at 15:25
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11752/discussion-between-david-b-and-luiggi-mendoza) – David B May 25 '12 at 16:49
5

Arrays.sort does not modify the variable, it modifies the array object the variable points to.

meriton
  • 68,356
  • 14
  • 108
  • 175
2

True, Java always does pass-by-value and for objects, the reference of the object by passed by value. Thus, the reference of your array is passed by value.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
1
  1. It sorts it.
  2. The reference to the array is passed by value. Not the array itself.
user207421
  • 305,947
  • 44
  • 307
  • 483
0

The reference to the array is passed by value, so the sort method has its own reference that still refers to the same array as a references. This means that sort can change the content of the same array that a references.

neilgmacy
  • 549
  • 5
  • 13