public class HelloJava{ public static void sub_1(){ String s1 = "Hello"; String s2 = s1; s1 = s1 + "world";//地址更改 System.out.println(s2); System.out.printf(s1); } public static void main(String[] args) { // TODO Auto-generated method stub sub_1(); } }
s1相加会新申请内存,重新指向一个新地址,而不是在原先地址处更改内存