我一直以为Java使用pass-by-reference. 但是,我读了一篇博客文章,声称Java使用pass-by-value. 我不认为我明白作者所做的区别。

什么是解释?


当前回答

Java 按值传输参考。

因此,你不能改变通过的参考。

其他回答

第二步,请记住,当您将对象转移到一种方法时,您将转移到对象参考,而不是对象本身。

现在,想想一个对象的参考/变量是什么:

1. Person person;
2. person = new Person("Tom");
3. changeName(person);
4.
5. //I didn't use Person person below as an argument to be nice
6. static void changeName(Person anotherReferenceToTheSamePersonObject) {
7.     anotherReferenceToTheSamePersonObject.setName("Jerry");
8. }

发生了什么?

变量人是创建的行 #1 它是零的开始. 一个新人对象是创建的行 #2,存储在记忆中,变量人是给予的参考人对象. 也就是说,其地址. 让我们说 3bad086a. 保持对象的地址的变量人是转移到函数行 #3. 在行 #4 你可以听到沉默的声音 查看其地址。

一张照片值得一千个字:

此分類上一篇

请注意,另一个ReferenceToTheSamePersonObject射线是指向对象而不是变量人!

您总是输入参考值的比特的副本!

Java 是 pass-by-value 因为在一个方法中,你可以随心所欲地修改所提到的对象,但无论你尝试多么艰难,你永远不会能够修改过去的变量,这将保持参考(不 p _ _ _ _ _ _ _ )相同的对象,不管是什么!



当涉及到对象时,对象本身不能转移到方法,所以我们通过对象的参考(地址)我们可以使用这个参考来操纵原始对象。

Account account1 = new Account();

此分類上一篇

如果我们将 array1 参考变量的值转换为反向Array 方法的论点,则在该方法中创建一个参考变量,而该参考变量则开始指向相同的序列(a)。

public class Test
{
    public static void reverseArray(int[] array1)
    {
        // ...
    }

    public static void main(String[] args)
    {
        int[] array1 = { 1, 10, -7 };
        int[] array2 = { 5, -190, 0 };

        reverseArray(array1);
    }
}

此分類上一篇

所以,如果我们说

array1[0] = 5;

我们有另一个参考变量在逆Array 方法(array2) 指向一个 array c. 如果我们要说

array1 = array2;

如果我们返回参考变量序列2作为逆序列方法的返回值,并将此值归咎于参考变量序列1在主要方法,序列1在主要将开始指向序列c。

public class Test
{
    public static int[] reverseArray(int[] array1)
    {
        int[] array2 = { -7, 0, -1 };

        array1[0] = 5; // array a becomes 5, 10, -7

        array1 = array2; /* array1 of reverseArray starts
          pointing to c instead of a (not shown in image below) */
        return array2;
    }

    public static void main(String[] args)
    {
        int[] array1 = { 1, 10, -7 };
        int[] array2 = { 5, -190, 0 };

        array1 = reverseArray(array1); /* array1 of 
         main starts pointing to c instead of a */
    }
}

此分類上一篇

在所有的答案中,我们看到Java通过价值,或者正如Gevorg所写的那样:“通过复制变量的价值”,这就是我们应该始终记住的想法。

在Java中,你总是通过复制论点,即你总是在函数中创建一个新的值例子,但有些行为会让你认为你正在通过参考。

此分類上一篇: [ref 1]

void incrementValue(int inFunction){
  inFunction ++;
  System.out.println("In function: " + inFunction);
}

int original = 10;
System.out.print("Original before: " + original);
incrementValue(original);
System.out.println("Original after: " + original);

We see in the console:
 > Original before: 10
 > In Function: 11
 > Original after: 10 (NO CHANGE)

以 [ref 2] 的例子

顯示優雅的機制觀點 max 5 min

(按参考通行) pass-by-copy-of-the-variable-值

void incrementValu(int[] inFuncion){
  inFunction[0]++;
  System.out.println("In Function: " + inFunction[0]);
}

int[] arOriginal = {10, 20, 30};
System.out.println("Original before: " + arOriginal[0]);
incrementValue(arOriginal[]);
System.out.println("Original before: " + arOriginal[0]);

We see in the console:
  >Original before: 10
  >In Function: 11
  >Original before: 11 (CHANGE)

复杂物体本身正在复制,但内部参考正在保持。

此分類上一篇: [ref 3]

package com.pritesh.programs;

class Rectangle {
  int length;
  int width;

  Rectangle(int l, int b) {
    length = l;
    width = b;
  }

  void area(Rectangle r1) {
    int areaOfRectangle = r1.length * r1.width;
    System.out.println("Area of Rectangle : " 
                            + areaOfRectangle);
  }
}

class RectangleDemo {
  public static void main(String args[]) {
    Rectangle r1 = new Rectangle(10, 20);
    r1.area(r1);
  }
}

我想分享的最后一件事是讲座的这个时刻:记忆分配,我认为它非常有用,以了解Java通过值或更为“通过复制变量的值”如Gevorg所写的。

Java 总是通过值的论点,而不是参考。


让我们用一个例子来解释这一点:

public class Main {

     public static void main(String[] args) {
          Foo f = new Foo("f");
          changeReference(f); // It won't change the reference!
          modifyReference(f); // It will modify the object that the reference variable "f" refers to!
     }

     public static void changeReference(Foo a) {
          Foo b = new Foo("b");
          a = b;
     }

     public static void modifyReference(Foo c) {
          c.setAttribute("c");
     }

}

我将在步骤中解释这一点:

宣布一个参考名为Foo类型,并将其分配给一个新的Foo类型对象与属性“f”。Foo f =新Foo(“f”);从方法侧,一个Foo类型的参考名称A被宣布,它最初被分配为零。

我希望你现在明白如何通过对象作为论点在Java工作:)

真的很简单,真的很简单:

对于原始类型的变量(如 int、boolean、char 等),当您使用其名称为方法论时,您将其中的值(5、 true、c)传输。

对于参考类型的变量(例如,线条,对象等),当您使用其名称为方法论时,您将其中的值转移(对象“点”的参考值)。这个参考值被“复制”,而变量在方法引用后仍然保持其值。

不管怎样,你总是通过价值的东西。


比较这个说 C++ 你可以有一个方法来采取一个 int& 或在 C# 你可以采取一个 ref int (虽然,在这种情况下,你也必须使用 ref 修改器,当将变量的名称转移到方法。