這是一段很有意思的代碼
String類的Contact()方法:
package com.party90.res;
public class test {
public static void main(String[] args) {
String str1 = "hello";
//這樣操作 str本身並不會改變,只有str1=str1.concat(" world"); 將新生成的對象引用賦給str1。
str1.concat(" world"); 以前的hello 就廢棄了。
String str2 = "hello";
str2 += " world";
str1.split(" ");
str2.split(" ");
for (int i = 0; i < str1.split(" ").length; i++) {
String s = str1.split(" ")[i];
System.out.print(s + "--");
}
System.out.println("\n");
for (int i = 0; i < str2.split(" ").length; i++) {
String s = str2.split(" ")[i];
System.out.print(s + "-");
}
}
}
---
輸出:
hello--
hello-world-
---
更改後輸出:
hello--world--
hello-world-
這樣就一致了。