这里不太明白!求解答
Topic source变量 s 实际保存的是字符串常量 hello 的地址,执行 t=s,只是将 s 保存的地址赋值给 t, 执行 s = "world" 后,是先在内存中创建字符串常量 world,然后把 world 的地址赋值给 s,这个时候 t 里面保存的地址还是字符串常量 hello 的地址。
S= hello,是指先在内存开辟个空间,保存hello,s指向hello,后来t=s,是指t和s一样指向hello,s = world,计算机又在内存里开辟一个空间保存world,s指向world,而之前的t还是指向hello
- 1
一只努力的小南瓜
public class Main {
public static void main(String[] args) {
String s = "hello";
String t = s;
s = "world";
System.out.println(t); // t是"hello"还是"world"?
}
}
这个为什么不是world? t 不是指向s了吗?,s又指向world了,所以t不应该是world吗?