简体   繁体   English

按值传递引用意味着什么?

[英]What does it mean for a reference to be passed by value?

I read in this question that Java is always pass-by-value . 在这个问题中读到Java 总是传递值 And so even references are passed by value. 因此,即使引用也按值传递。

I don't understand what this means, can somebody clarify this for me please? 我不明白这是什么意思,请有人为我澄清一下吗?

Given this 鉴于这种

Object ref = new Object();

ref is actually storing a value, some address to an object. ref实际上是存储一个值,即对象的一些地址。 Let's say 1234 . 假设1234

When you pass ref around 当您传递ref

public void method(Object passed) {...}
...
method(ref);

Java actually copies the value of the reference and assigns it to the parameter. Java实际上会复制引用的值并将其分配给参数。 So, passed will also have the value 1234 . 因此, passed也将具有值1234

Similarly, if you had 同样,如果您有

Object otherRef = ref;

the value 1234 would be copied and assigned to otherRef . 1234将被复制并分配给otherRef

If you then re-assign otherRef , like 如果您然后重新分配otherRef ,例如

otherRef = new Object();

that would assign a new value to otherRef , but ref would still have the same value as before. 会为otherRef分配一个新值,但ref仍具有与以前相同的值。 That's what pass by value is. 这就是价值传递。

When you call a method 调用方法时

ref.toString();

Java uses the value of the reference to find the referenced object and invoke the method. Java使用引用的值来查找被引用的对象并调用该方法。 This is called dereferencing. 这称为取消引用。


You might want to go through the JPDA javadoc, starting with StackFrame . 您可能想要遍历JPDA javadoc,从StackFrame开始。 Go through the fields and types and you'll start to understand how everything is mapped. 浏览这些字段和类型,您将开始了解如何映射所有内容。 For example, it has a getValues(..) method which returns a Map<LocalVariable, Value> . 例如,它具有一个getValues(..)方法,该方法返回Map<LocalVariable, Value> That should tell you that a variable doesn't actually store anything. 那应该告诉你变量实际上并没有存储任何东西。 Instead, it is mapped to a value, where that value may be all sorts of things . 相反,它映射到一个值,该值可能是各种各样的东西

int a = 5;

public void foo(int num) {
num = num + 5;
System.out.println(num);
}

foo(a);

System.out.println(a);

In the above code, a is passed into foo() by value. 在上面的代码中,a通过值传递到foo()中。 That means that a new variable is created with the scope of foo() that has the same initial value as a. 这意味着将使用foo()的范围创建一个具有与a相同的初始值的新变量。 When the value of num is changed, the value of a is not changed. 当num的值更改时,a的值不变。 The first println() will print a 10, the second will print a 5. In c++ you could pass a in by reference, which means that the value of a would be changed too. 第一个println()将输出10,第二个println()将输出5。在c ++中,您可以通过引用传递in,这意味着a的值也将被更改。

I can try, in some other languages you have the option of passing something by a pointer (eg a reference to a memory region). 我可以尝试,在其他一些语言中,您可以选择通过指针传递某些内容(例如,对内存区域的引用)。 Java calls every function with a value (not a pointer), but the value of an Object is a reference to a memory region. Java用一个值(不是指针)调用每个函数,但是Object的值是对内存区域的引用。 See also the default toString() in your own classes. 另请参见您自己的类中的默认toString()

I always find this a good example: 我总是找到一个很好的例子:

Dog foo = new Dog("Rocky");
modifyDog(foo);
System.out.println(foo.name); //Alice

public void modifyDog(Dog aDog)
{
   aDog.name = "Alice"; //here you change the field 'name' of the object located in memory currently referenced by foo and aDog variables. 
   aDog =  new Dog();   //since aDog is a copy of foo then this won't recreate foo object
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM