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 と同じように動作することを望みました - 与えられた参照に従って変更します。
この別のオブジェクトへのオブジェクト参照が、整数や文字列などではなく、ユーザー定義オブジェクトでのみ「機能する」のはなぜですか?