Jishnu Prathap Jishnu Prathap - 1 year ago 82
Java Question

Can we use pass by reference in java? If No How does java.util.Arrays.sort works?

I used to think Java supports both pass by value and passby reference but i came accross many discussions like

  1. Java is always pass by value, with no exceptions, ever .

  2. Java is always pass-by-value

  3. Java always passes arguments by value NOT by reference.

  4. Java passes references by value.So you can't change the reference that gets passed in.

If java only supports pass by value
how does

int iArr[] = {2, 1, 9, 6, 4};// sorting array


System.out.println("The sorted int array is:");
for (int number : iArr) {
System.out.println("Number = " + number);

What passing a reference (by value) Actually mean? How does it differ from passing by reference behaviour of Arrays in C or C++?

Please correct me if I am wrong. In C we pass the address of variables when passing by reference.In Java we pass the reference to the object (or value).As long as the variable in the method is pointing to the Object the value of the object changes with the varible in the method invoked.
Java Pass by reference
There is no copy of Object or reference made!, I could see only 2 different variables pointing to the same Object as in pass by reference.Similar in C++ pass by reference two different variables points to the same address.

Answer Source

Arrays are reference types, so the iArr variable holds a reference to an array.

In other words, when you call


you're passing a reference (by value) to the sort method, which sorts the array that iArr refers to.

From comments:

What does passing a reference (by value) actually mean?

What pass by reference means is that you're basically passing the variable itself to the method. I.e., what ever the method does with the variable affects the variable on the outside. This is never the case in Java. (Try implementing a swap method and you'll see what I mean.) Passing by value means that you pass the value that's stored in the variable. In this case the value is a reference, so it's passing a reference by value.

Re. second update:

Judging from your image, I think you've understood the situation very well, and I think it boils down to terminology.

If we forget about C++ for a while, it's really simple. All you need to keep in mind is that (A) when you invoke method(var) the argument is a copy of whatever var contains, and (B) the content of a non-primitive variable is a reference (a "pointer" if you so like).

Note that in your question you have

int iArr[] = {2, 1, 9, 6, 4};

which is equivalent to

int[] iArr = new int[] { 2, 1, 9, 6, 4 };

so it all checks out: iArr holds a reference and new returns a reference.

When you invoke Arrays.sort(iArr) the content of iArr is passed (i.e. the reference to the array). This is still not pass-by-reference because the value is passed, not the variable itself. If you reassign the formal parameter inside the method to point to some other array, iArr will still point to the original array when the method returns.

If we do think in terms of C++ things tend to be a bit more complicated; C++ notion of reference is slightly different. With a C++ reference you can in fact implement a real swap:

void swap(int &x, int &y)
   int temp = x;
   x = y;
   y = temp;

I.e. you can pass in "a variable" (as opposed to just the content of a variable). I like to think of this as you're sharing the scope of the variable with the method you're calling. This can't be done in Java.

So with that in mind, I'd say Java reference are much more like C++ pointers, except that they are limited in the sense that you can't dereference using * operator as you can in C++ (you can't do *person in Java, even though person stores what corresponds to a pointer to a person) and you can't get the address of an object using & operator. Also you can't do any pointer arithmetic. You can't for instance do iArr + 3 to get to the fourth element of your array.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download