我们可以在java中使用按引用传递吗?如果否 java.util.Arrays.sort 如何工作? [英] Can we use pass by reference in java? If No How does java.util.Arrays.sort works?

查看:35
本文介绍了我们可以在java中使用按引用传递吗?如果否 java.util.Arrays.sort 如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经认为 Java 支持传递值和传递引用,但我遇到了许多讨论,例如

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

  1. Java 总是按值传递,从来没有例外.
  2. Java 总是按值传递
  3. Java 总是按值而不是按引用传递参数.
  4. Java 按值传递引用.因此您无法更改传入的引用.

如果 java 只支持按值传递
java.util.Array.sort()Collections.sort(unsortList) 如何工作?

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);
}

更新:传递引用(按值)实际上是什么意思?它与 C 或 C++ 中数组的引用传递行为有何不同?

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

更新:如果我错了,请纠正我.在C中我们通过引用传递时传递变量的地址.在Java中我们传递对对象(或值)的引用.只要方法中的变量指向Object,对象的值就会随着变量的变化而变化调用的方法.没有对象的副本或引用!,我只能看到 2 个不同的变量指向同一个对象,因为在 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.

推荐答案

数组是引用类型,所以 iArr 变量保存对数组的引用.

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

换句话说,当你打电话

Arrays.sort(iArr);

您将引用(按值)传递给 sort 方法,该方法对 iArr 引用的数组进行排序.

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

来自评论:

传递引用(按值)实际上意味着什么?

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

引用传递意味着您基本上将变量本身传递给方法.即,该方法对变量所做的任何事情都会影响外部的变量.在 Java 中从来没有这种情况.(尝试实现一个交换方法,你就会明白我的意思.)按值传递意味着你传递存储在变量中的值.在这种情况下,该值是一个引用,因此它是按值传递一个引用.

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.

再.第二次更新:

从你的形象来看,我认为你已经非常了解情况了,我认为这归结为术语.

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

如果我们暂时忘记 C++,它真的很简单.所有你需要记住的是 (A) 当你调用 method(var) 时,参数是 var 包含的任何内容的副本,以及 (B) 的内容一个非原始变量是一个引用(如果你愿意,一个指针").

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

请注意,在您的问题中

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

相当于

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

所以这一切都检查出来了:iArr 持有一个引用,new 返回一个引用.

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

当您调用 Arrays.sort(iArr) 时,会传递 iArr 的内容(即对数组的引用).这仍然不是按引用传递,因为传递的是值,而不是变量本身.如果将方法内部的形参重新赋值为指向其他数组,当方法返回时iArr 仍将指向原始数组.

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.

如果我们确实从 C++ 的角度考虑,事情往往会更复杂一些;C++ 的引用概念略有不同.使用 C++ 参考,您实际上可以实现真正的 swap:

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;
}

即您可以传入变量"(而不仅仅是变量的内容).当您与正在调用的方法共享变量的作用域时,我喜欢考虑这一点.Java 无法做到这一点.

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.

因此,考虑到这一点,我会说 Java 引用更像 C++ 指针,只是它们的局限性在于您不能像在使用 * 运算符那样取消引用C++(你不能在 Java 中做 *person,即使 person 存储对应于一个人的指针的东西)并且你不能得到一个对象的地址使用 & 运算符.你也不能做任何指针运算.例如,您不能执行 iArr + 3 来获得数组的第四个元素.

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.

这篇关于我们可以在java中使用按引用传递吗?如果否 java.util.Arrays.sort 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆