Java-将列表传递给作为通过引用传递的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29726423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:47:56  来源:igfitidea点击:

Java- passing Lists to methods working as pass by reference

java

提问by alekya reddy

There are lot of discussions in stackoverflow regarding pass by value and pass by reference. But i want to know what is happening in the following scenario? This page says java is pass by value. Is Java "pass-by-reference" or "pass-by-value"?.

stackoverflow 中有很多关于传值和传引用的讨论。但我想知道在以下场景中发生了什么?这个页面说java是按值传递的。Java 是“按引用传递”还是“按值传递”?.

In case of following code, the the element is removed from the removeElement method , it is removing the 5th element from list when i print the list.

在以下代码的情况下,元素从 removeElement 方法中删除,当我打印列表时,它从列表中删除第 5 个元素。

public class Load {
    public static void main(String[] args) {


        ArrayList<Integer> list = new ArrayList<Integer>();
        list.addAll(Arrays.asList(1,1,2,3,5,5,13,21));
        removeElement(list);
        System.out.println(list);
    }
        public static void removeElement(List<Integer> list){
            list.remove(5);//removes element at index 5
        }
}

The output of the program is [1, 1, 2, 3, 5, 13, 21].

程序的输出是 [1, 1, 2, 3, 5, 13, 21]

Can somebody explain how this is pass by value rather than pass by reference?

有人可以解释这是如何按值传递而不是按引用传递吗?

采纳答案by Seelenvirtuose

Although this question asks (again) for the difference between "pass-by-value" and "pass-by-difference", I think there is an underlying misconception that causes so much confusion. This must be resolved:

尽管这个问题(再次)询问“传递值”和“传递差异”之间的区别,但我认为存在导致如此多混乱的潜在误解。这必须解决:

As a matter of fact a method call passes all parameters by value. As Erwin Bolwidt pointed out in his comments, there are two kinds of types:

事实上,方法调用按值传递所有参数。正如 Erwin Bolwidt 在他的评论中指出的那样,有两种类型:

  • primitive types
  • reference types
  • 原始类型
  • 引用类型

The JLS (§4)states it:

JLS(§4)称它:

The Java programming language is a statically typed language, which means that every variable and every expression has a type that is known at compile time.
[...]
There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3).

Java 编程语言是一种静态类型语言,这意味着每个变量和每个表达式都有一个在编译时已知的类型。
[...]
有两种类型的Java编程语言:基本类型(4.2节)和引用类型(第4.3节)。

When speaking about "pass-by-value", it means that the content of a variable is passed by value when being used as a parameter for a method call. And the content of a reference typed variable is a reference. It is not the object that is referred to by a variable. That's a huge difference.

当谈到“按值传递”时,它意味着变量的内容在用作方法调用的参数时按值传递。而引用类型变量的内容就是引用。变量引用的不是对象。这是一个巨大的差异。

In your example, the content of the variable listis passed as value to the method removeElement. This valueis the referenceto the list object.

在您的示例中,变量的内容list作为值传递给方法removeElement。该是对列表对象的引用

That further means that the method removeElementnow has access to that list object (by its reference) and can manipulate it, for example remove an element. But it has no access to the content of the variable with which the method was called. So it cannot change the content of the variable listinside the mainmethod (which would be possible with "pass-by-reference").

这进一步意味着该方法removeElement现在可以访问该列表对象(通过其引用)并可以操作它,例如删除一个元素。但是它无法访问调用该方法的变量的内容。所以它不能改变方法list内变量的内容main(这可以通过“传递引用”来实现)。

回答by Elliott Frisch

Java is always pass by value. The value of any variableof type Objectis actually a reference. That's why, for example, ==is said to be a reference comparison, and you need to use .equals()for comparing Object(s).

Java 总是按值传递。任何类型变量的值Object实际上都是一个引用。这就是为什么,例如,==说是一个参考比较,你需要使用.equals()进行比较Object(s)。

回答by Aniket Thakur

In case of primitives it purely pass by valueand in case of Objectsit is pass by value of the reference.

在原语的情况下,它纯粹是pass by valueObjects它的情况下pass by value of the reference

When you pass listfrom the main method to method removeElement()as an argument there is another (copy) reference that is created that points to the same List instance. Any changes made from either of the references will reflect on the same actual instance. However if you assign one of the reference to some new Object the other reference will still point to the same original instance.

当您将listmain 方法removeElement()作为参数传递到方法时,会创建另一个(复制)引用,该引用指向同一个 List 实例。对任一引用所做的任何更改都将反映在同一个实际实例上。但是,如果您将一个引用分配给某个新对象,另一个引用仍将指向同一个原始实例。