public class Test {
public static void main(String[] args) {
String s = "hello";
String t = s;
s = "world";
System.out.println(t); //t是"hello"
System.out.println(s); //这里的s是"world"
}
}
变量 t 与第一个 s 一致,所以打印变量得出的是 "hello" ;第二个 s 则是新的指向,所以打印 s 的结果是 "world" 。
祭海
略改动了一下打印的内容:
变量 t 与第一个 s 一致,所以打印变量得出的是 "hello" ;第二个 s 则是新的指向,所以打印 s 的结果是 "world" 。