【问题标题】:Comparing Objects which refer to each other in Java比较Java中相互引用的对象
【发布时间】:2013-04-28 10:00:30
【问题描述】:
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 相同 - 根据给出的参考进行更改。

为什么这个对象引用到另一个对象只“适用于”用户定义的对象,而不是整数、字符串等?



谢谢大家的回答 - 现在我明白了!

【问题讨论】:

    标签: java object compare refer


    【解决方案1】:

    StringInteger 对象在 Java 中是不可变的。当您这样做时:ii += 1k += "tring" 您会创建新对象。变量jjl 指向旧对象。这就是为什么您会看到不同的值。

    【讨论】:

      【解决方案2】:

      这种行为与用户定义的对象与内部对象无关,而是“整数”和“字符串”(以及所有其他原始包装器)对象的处理非常特殊。 Integer 只是原始整数的“包装器”,因此它的行为并不是真正的“引用类型”。所有这些包装器对象都是不可变的——对 Integer 的相同引用永远不会有另一个值。

      附带说明:如果需要,这些包装器对象会自动转换为原始类型,因此它们在一般使用中速度较慢。它们的好处是,它们可以为 null,这有时很好。

      【讨论】:

        【解决方案3】:

        这与+= 运算符的工作有关。

        你真正做的是在你调用+=时重新分配值

        Integer i = 1;
        Integer j = i;
        i = i + 1;
        

        所以现在i 指向另一个等于2Integerj 仍然指向等于Integer 的原始Integer

        String 示例也是如此。

        在您的对象情况下,您不会更改指针,而是更改对象的内部状态。因此person1person2 都指向同一个Person

        如果你这样做了

        Person person1 = new Person("Alice");
        Person person2 = person1;
        person1 = new Person("Bob");
        

        那么很明显person1 现在是一个不同 Person

        【讨论】:

        • 第一个例子,我怎么能等于2?不应该是1吗?
        • 谢谢你的回答:)
        【解决方案4】:

        1)Strings 和 Wrappers(Integer, Long..) 是不可变对象,并且 2)Person 是一个可变对象,您修改了它的属性,并且由于 person1 和 person2 指向相同的引用,因此更改适用于两个对象。

        (不可变——一旦创建就不能改变它的状态)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-09
          • 2016-09-12
          相关资源
          最近更新 更多