Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 java.util.Array.sort() or Collections.sort(unsortList) work?

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

Arrays.sort(iArr);    

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

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

Update: 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.

like image 873
Jishnu Prathap Avatar asked May 22 '15 09:05

Jishnu Prathap


People also ask

Is Java pass by reference for arrays?

Arrays are Objects, yes, but nothing in Java is passed by reference. All parameter passing is by value. In the case of an Object, what gets passed is a reference to the Object (i.e. a pointer), by value. Passing a reference by value is not the same as pass by reference.

Does Java allow pass by reference?

Java is always Pass by Value and not pass by reference, we can prove it with a simple example. Let's say we have a class Balloon like below. And we have a simple program with a generic method to swap two objects, the class looks like below.

How do you pass an array of references to a function in Java?

To pass an array to a function, just pass the array as function's parameter (as normal variables), and when we pass an array to a function as an argument, in actual the address of the array in the memory is passed, which is the reference.

How do you pass reference by reference in Java?

In order to pass the reference, we pass the object of the class in the place of the actual parameter and the formal parameter of a class object type has the same reference to each other that's why with the help of the formal parameter object of class any changes will be reflected in both objects formal and actual ...


2 Answers

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

In other words, when you call

Arrays.sort(iArr);

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.

like image 123
aioobe Avatar answered Nov 05 '22 17:11

aioobe


Java always uses pass by value. Pass by value means the 'value' is copied when it's passed. When it comes to passing objects the 'value' that gets copied is the 'reference' to the object, not the object it self. Therefore if you were to make changes to the object inside a method, those would get reflected after method execution. However setting the passed 'reference' to 'null' for example would have no effect.

like image 31
Dev Blanked Avatar answered Nov 05 '22 16:11

Dev Blanked