int i = 0;
int j = i;
System.out.println("initial int: " + j); // 0
Integer ii = new Integer(0);
Integer jj = ii;
System.out.println("initial Integer: " + jj); // 0
String k = new String("s");
String l = k;
System.out.println("initial String: " + l); // "s"
Person person1 = new Person("Furlando"); // from constructor -> publ. instance var. 'name'
Person person2 = person1;
System.out.println("initial Person: " + person2.name); // "Furlando"
/*--------------------*/
System.out.println();
/*--------------------*/
i += 1;
System.out.print("added 1 to int: [" + i);
System.out.println("], and primitive which also \"refers\" to that (has a copy, actually), has a value of: [" + j + "]");
ii += 1;
System.out.print("added 1 to Integer object: [" + ii);
System.out.println("], and object which also refers to that, has a value of: [" + jj + "]");
k += "tring";
System.out.print("added \"s\" to String object: [" + k);
System.out.println("], and object which also refers to that, has a value of: [" + l + "]");
person1.name = "Kitty";
System.out.print("changed instance variable in Person object to: [" + person1.name);
System.out.println("], and object which also refers to that, has a value of: [" + person2.name + "]");
/* [COMPILER OUTPUT]
initial int: 0
initial Integer: 0
initial String: s
initial Person: Furlando
A) added 1 to int: [1], and primitive which also "refers" to that (has a copy, actually), has a value of: [0]
B) added 1 to Integer object: [1], and object which also refers to that, has a value of: [0]
C) added "s" to String object: [string], and object which also refers to that, has a value of: [s]
D) changed instance variable in Person object to: [Kitty], and object which also refers to that, has a value of: [Kitty]
*/
我理解 A,我们在那里有一个原语;没有参考。复制。
我希望 B 和 C 的行为方式与 D 相同 - 根据给出的参考进行更改。
为什么这个对另一个对象的对象引用仅适用于用户定义的对象,而不是整数、字符串等?