views:

233

answers:

5

I thought I'm pretty experienced in java, but it seems that's not really the case, I just noticed something yesterday, something I used before but never really realised what it did. I googled but didn't find the answer to my question.

If I declare an int array, and use Array's static sort function to sort my array, I just need to type

Arrays.sort( numbers );

Instead of

numbers = Array.sort( numbers );

This might look very easy in C and C++ because you can use pointers there. So what I'm wondering is, how is this done? Is it an advantage sun has, or am I completely senseless here?

+6  A: 

First, this belongs on StackOverflow. Second, you want to read the article Java is Pass-by-Value, Dammit!

joschi
This doesnt answer the question at all!
Paul Biggar
Actually it does. `sort()` gets a copy of a reference to the array. It can re-arrange the *contents* (i. e. the state) of the array as it pleases. It *can't* create a new one and assign it to the one you passed. But that's not what happens here.
Joey
It does, but a much simpler explanation is "a reference to the object is passed to the `sort` function. Whether the reference is copied or passed by reference is irrelevant for this question. All that matters is that what the function sees is a reference to the original object, not a copy of the object.
jalf
+2  A: 

I assume numbers is an int-array, and all arrays in Java are objects. So sort is passed a reference to numbers, and it can sort them in place.

Paul Biggar
+2  A: 

This question should be migrated to Stack Overflow. The fact that Java has a NullPointerException class should give you a strong hint as to whether Java uses pointers behing the scenes.

John Topley
Yes, but `NullPointerException` really has a wrong name - it would have been better if it was something like `NullReferenceException` or `IllegalNullDereferenceException` or something like that. References don't necessarily have to be implemented by pointers behind the scenes.
Jesper
I agree that it's inconsistent naming by Sun. However, you'd be pretty hard pressed to implement Java without the concept of variables that refer to memory addresses (i.e. pointers) under the hood.
John Topley
+7  A: 

Pointers exist in java - all non primitive variables are pointers in java, aka references.

THey do not support the same set of operations as pointers in C tho - they are essentially opaque to the user of the language.

The reason Arrays.sort(array) works is because array is a "pointer", which allows the sort() function access to the memory location that the array variable points to.

Now, why doesn't:

void swap (Integer a, Integer b) {
   Integer tmp = a;
   a = b;
   b = tmp;
}

work if you did

Integer x = 1;
Integer y = 2;
swap(x,y);

Its because java passes by value (which is a concept distinct from pointers). The pointer to 1 is given to swap(), not as the value of the variable x (which is a memory address, or pointer). Thus, manipulating the arguments in swap() does nothing to effect the variable x.

Chii
http://javadude.com/articles/passbyvalue.htm in joschi's answer is a great article to read on this topic
Chii
+4  A: 

It sorts the array in-place.

The sort method receives a reference to the number array (which is an object), and changes the values inside the array! No new array-object is created, it is thus good enough to just pass it to the sort function.

 

PS: all source code of Java is open, you can go and read the sources of the sort function yourself. You'll see, there be no magic. If Java is installed properly on your system, there should be a src.zip in the Java home folder.

Adrian
It might also be useful to note that arrays are always objects, even arrays of primitive types. You could not do this with a single primitive, for example: void increaseInt(int value) would not work, the original int cannot be changed by anything done inside the method.
Adriaan Koster
@adriaan added parens.
Adrian