英文:
Java String objects equality and reference
问题
我有以下代码:
public class Test{
public static void main(String []args){
String x = "a";
String y = "a";
System.out.println(System.identityHashCode(x) == System.identityHashCode(y)); //true
System.out.println(x.hashCode() == y.hashCode()); //true
y = y + "b";
System.out.println(x); // "a"
System.out.println(y); // "ab"
System.out.println(System.identityHashCode(x) == System.identityHashCode(y)); //false
System.out.println(x.hashCode() == y.hashCode()); //false
}
}
首先,我创建了两个字符串x和y。然后我检查它们的HashCode,它们相同,这意味着它们是同一个对象,并且它们指向同一个内存位置。但是当我改变y的值时,x的值不会改变。如果我再次检查它们的HashCode,它们是不同的,这意味着它们是两个不同的对象,指向不同的内存位置。为什么会发生这种情况?为什么y改变时x不会改变?(因为我们只是改变了一个内存的内容)
我按照这个问题中的建议使用了HashCode。
更新: 我的困惑有两个原因:
a) 我以为相同的HashCode意味着相同的对象(你可以查看这个问题和这个问题,详细解释了我为什么错了。)
b) 字符串是不可变的,如果值改变,会创建新的字符串(如下面的回答中所解释的。)
英文:
I have the following code:
public class Test{
public static void main(String []args){
String x = "a";
String y = "a";
System.out.println(System.identityHashCode(x) == System.identityHashCode(y)); //true
System.out.println(x.hashCode() == y.hashCode()); //true
y = y+"b";
System.out.println(x); // "a"
System.out.println(y); // "ab"
System.out.println(System.identityHashCode(x) == System.identityHashCode(y)); //false
System.out.println(x.hashCode() == y.hashCode()); //false
}
}
First I create 2 Strings x and y. Then I check their HashCodes and they are same so it means they are one object and they point to one memory location. But when I change y value, The value of x won't change. If I check their hashcode again, They are different so that means 2 different objects and 2 different memory locations. Why does that happen? Why doesn't x change when y changes? (because we are just changing the content of one memory)
I used hashcode as this question suggests.
Update:
My confusion was for 2 reasons:
a) I thought same hashcodes mean same objects (You can have a look at this and this questions for the detailed explanation why I'm wrong.)
b) Strings are immutable and if the value changes, New Strings are created (As explained in the answers below.)
答案1
得分: 4
在Java中,字符串是不可变的。您并没有“更改y的值”,而是创建了一个新的字符串并将其赋值给y。由于您没有将其他任何内容分配给x,它仍将引用先前存在的字符串。
英文:
Strings in Java are immutable. You aren't "changing the value of y", you're creating a new string and assigning it to y. Since you haven't assigned anything else to x, it will still reference the previous string you had there.
答案2
得分: 2
我认为这个问题最终源自于您误解了x
和y
是对象。x
和y
并不是对象。
x
和y
是_变量_,由于String
是引用类型,x
和y
存储_引用_,指向对象。=
赋值操作符会改变这些变量存储的引用。
在这两行中:
String x = "a";
String y = "a";
x
和y
存储引用,这些引用指向相同的对象。
但是当您执行y = y + "b"
时,y + "b"
创建了一个_新对象_。然后 =
使 y
存储对该新对象的引用,因此 y
不再指向与 x
相同的对象。
英文:
I think this question ultimately stems from your misunderstanding that x
and y
are objects. x
and y
are not objects.
x
and y
are variables and since String
is a reference type, x
and y
store references to objects. The =
assignment operator changes what reference these variables store.
In these two lines:
String x = "a";
String y = "a";
x
and y
store references that refers to the same object.
But when you do y = y + "b"
, the y + "b"
creates a new object. Then the =
make y
store a reference to that new object, so y
no longer points to the same object as x
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论