Discuss / Java / 🖊笔记-变量的指向

🖊笔记-变量的指向

Topic source

祭海

#1 Created at ... [Delete] [Delete and Lock User]

略改动了一下打印的内容:

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" 。


  • 1

Reply