Can we use pass by reference in java? If No How do

2019-06-24 00:22发布

问题:

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

回答1:

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.



回答2:

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.



回答3:

Java is always pass by value. But passing parameter value depends on type of entity you are passing..

Primitives - The original value is passed. So changes done in the called method wont affect the original value in calling method.

Objects - The memory address(i.e. location in the heap memory) referring the object is passed. So changes done in the called method reflects in calling method.



回答4:

Java supports pass by value only.Its doesn't supports pass by reference.

See the below Code:

 public static void main(String[] args) {
List l1 = new ArrayList(Arrays.asList(4,6,7,8));
System.out.println("Printing list before method calling:"+ l1);
foo(l1);
System.out.println("Printing list after method calling:"+l1);
}

public static void foo(List l2) {
l2.add("done"); // adding elements to l2 not l1
l2.add("blah");
}

It looks like pass by reference. But internally works as by value only.

Output:

Printing list before method calling:[4, 6, 7, 8]
Printing list after method calling:[4, 6, 7, 8, done, blah]

This example is almost similar to your post. You are passing array to Arrays method like Arrays.sort(iArr). I am passing list parameters to my method foo ie: foo(list1).

See the post for more information

Java pass by reference issue with List