我一直以为Java使用pass-by-reference. 但是,我读了一篇博客文章,声称Java使用pass-by-value. 我不认为我明白作者所做的区别。
什么是解释?
我一直以为Java使用pass-by-reference. 但是,我读了一篇博客文章,声称Java使用pass-by-value. 我不认为我明白作者所做的区别。
什么是解释?
当前回答
Java 总是使用 Call by Value. 这意味着该方法获得所有参数值的副本。
考虑下列三种情况:
1、试图改变原始变量
public static void increment(int x) { x++; }
int a = 3;
increment(a);
x 将复制一个值,并将增加 x,一个将保持相同的值
(二)试图改变对象的原始领域
public static void increment(Person p) { p.age++; }
Person pers = new Person(20); // age = 20
increment(pers);
p 将复制个体的参考值,并将增加年龄领域,变量是指相同的对象,所以年龄改变。
(三)试图改变参考变量的参考值
public static void swap(Person p1, Person p2) {
Person temp = p1;
p1 = p2;
p2 = temp;
}
Person pers1 = new Person(10);
Person pers2 = new Person(20);
swap(pers1, pers2);
呼叫交换 p1 后,p2 复制 pers1 和 pers2 的参考值与值交换,因此 pers1 和 pers2 仍然相同。
因此,您只能在将参考值复制到该对象时更改对象的字段。
其他回答
Bob 保持他的列表,他称之为 tinyHouseAt1234Main,与相机相机,允许他在实时注意到实际房子的任何变化。
这个奇怪的副作用,通过一个列表到新的自动化服务是混乱的人谁问它是如何工作的。 事实上,什么是区别的能力远程控制机器人,改变的状态的房子在1234 Main, vs. 实际上物理上去那里,因为安妮给了你地址?
一般来说,Java有原始类型(int、bool、char、double等)直接通过值,然后Java有对象(所有来自java.lang.Object的一切)。对象实际上总是通过参考处理(一个参考是指标,你不能触摸)。这意味着实际上,对象通过参考,因为参考通常并不有趣。
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工作:)
Java 按值传输参考。
因此,你不能改变通过的参考。
Java 编程语言仅通过值的论点,也就是说,您无法从所称方法中更改呼叫方法中的论点值。
但是,当一个对象例子作为论点转移到一种方法时,论点的价值不是对象本身,而是对象的参考。
对于许多人来说,这似乎是通过参考,行为上,它与通过参考有很多共同点,但是,有两个原因,这是不准确的。
第一,改变已转化为一种方法的能力仅适用于对象,而不是原始价值;第二,与对象类型变量相关的实际价值是对象的参考,而不是对象本身。
The following code example illustrates this point:
1 public class PassTest {
2
3 // Methods to change the current values
4 public static void changeInt(int value) {
5 value = 55;
6 }
7 public static void changeObjectRef(MyDate ref) {
8 ref = new MyDate(1, 1, 2000);
9 }
10 public static void changeObjectAttr(MyDate ref) {
11 ref.setDay(4);
12 }
13
14 public static void main(String args[]) {
15 MyDate date;
16 int val;
17
18 // Assign the int
19 val = 11;
20 // Try to change it
21 changeInt(val);
22 // What is the current value?
23 System.out.println("Int value is: " + val);
24
25 // Assign the date
26 date = new MyDate(22, 7, 1964);
27 // Try to change it
28 changeObjectRef(date);
29 // What is the current value?
30 System.out.println("MyDate: " + date);
31
32 // Now change the day attribute
33 // through the object reference
34 changeObjectAttr(date);
35 // What is the current value?
36 System.out.println("MyDate: " + date);
37 }
38 }
This code outputs the following:
java PassTest
Int value is: 11
MyDate: 22-7-1964
MyDate: 4-7-1964
The MyDate object is not changed by the changeObjectRef method;
however, the changeObjectAttr method changes the day attribute of the
MyDate object.