Can we use reference pass in java? If not How does java.util.Arrays.sort work?

I used to think that Java supports both pass-by-value and passby reference, but I came across many discussions like

If java only supports pass by value, how does java.util.Array.sort()

or work Collections.sort(unsortList)

?

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 does link mean (by value)? How does it differ from traversing the reference behavior of arrays in C or C ++?

Update: Please correct me if I am wrong. In C, we pass the address of the variables when passed by reference. In Java, we pass a reference to an object (or value). As long as a variable in a method points to an object, the value of the object is modified by the variable in the called method. Java Pass by reference No object copy or reference !, I could only see two different variables pointing to the same object, just like in the reference pass. Similar to C ++, by reference, two different variables point to the same address.

+3


source to share


4 answers


Arrays are reference types, so the variable iArr

contains a reference to the array.

In other words, when you call

Arrays.sort(iArr);

      

you are passing a reference (by value) to the sort method, which sorts the array that it refers to iArr

.


From comments:

What does link passing (by value) mean?

What is passed by reference means that you are basically passing that variable to the method itself. Ie that ever a method with a variable affects the variable outside. This is never the case in Java. (Try to implement the swap method and you'll see what I mean.) Passing by value means that you are passing the value that is stored in a variable. In this case, the value is a reference, so it passes the reference by value.


Re. second update:

Judging by your image, I think you understand the situation well and I think it comes down to terminology.



If we forget about C ++ , it's very simple. All you need to keep in mind is that (A) when you call method(var)

, the argument is a copy of either var

, and (B) the content of the non-primitive variable is a reference ("pointer") if you like).

Please note that in your question you have

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

      

which is equivalent

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

      

so everything is checked: iArr

contains a link and new

returns a link.

When called Arrays.sort(iArr)

, the content is passed iArr

(i.e. a reference to an array). This is still not passed by reference, because the value is passed, not the variable itself. If you reassign a formal parameter inside a method to point to some other array, iArr

it will still point to the original array when the method returns.

If we think in C ++ terms , things tend to be a little more complicated; C ++ concept of a link is slightly different. With a C ++ reference, you can actually implement the real one swap

:

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

      

those. you can pass a "variable" (as opposed to the contents of a variable). I like to think about this as you are sharing the scope of the variable with the method you call. This cannot be done in Java.

So with that in mind, I would say that Java references are much more like pointers in C ++, except that they are limited in the sense that you cannot play with an operator *

like you can in C + + (you can "t do *person

in Java, although person

stores that corresponds to the index on the person), and you can not get the address of an object with the help of the operator &

. also, you can not perform any arithmetic pointer. you can not, for example, do iArr + 3

to go to the fourth element of your array.

+11


source


Java always uses pass by value. Pass by value means that if the value is "value" is copied. When it comes to passing objects, the "value" that is copied is a "reference" to the object, not the object. Therefore, if you had to make changes to the object inside the method, they will be reflected after the method is executed. However, setting the passed "reference" to "zero", for example, would have no effect.



+3


source


Java only supports skipping by value. It does not support passing by reference.

See 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 passing by reference. But internally only works by value.

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 an array to the Arrays method for example Arrays.sort(iArr)

. I am passing the list parameters to my foo method i.e foo(list1)

. : .

See post for more information

Java passing by reference combobox

+2


source


Java always passes a value . But passing the parameter value depends on the type of object you are passing.

Primitives - The original value is passed. This way, changes made in the called method will not affect the original value in the calling method.

Objects - The memory address (i.e., the location in the heap memory) that the transmitting object is. Thus, changes caused by the called method are reflected in the calling method.

+2


source







All Articles