1 /* 2 創建字符串的常見3+1種方式。 3 三種構造方法: 4 public String():創建一個空白字符串,不含有任何內容 5 public String(char[] array):根據字符數組的內容,來創建對應的字符串。 6 public String(byte[] array):根據字節數組的內容,來創建對應的字符串。 7 一種直接創建: 8 String str = "Hello"; //右邊直接用雙引號 9 */ 10 public class Demo02 { 11 public static void main(String[] args){ 12 //使用空參構造 13 String str1 = new String();//小括號留空,說明字符串說明內容都沒有 14 System.out.println("第1個字符串" +str1); 15 16 //根據字符數組創建字符串 17 char[] charArray = {'A','B','C'}; 18 String str2 = new String(charArray); 19 System.out.println("第2個字符串" +str2) 20 ; 21 //根據字節數組的內容,來創建對應的字符串 22 byte[] byteArray = {97,98,99}; 23 String str3 = new String(byteArray); 24 System.out.println("第3個字符串" +str3); 25 } 26 }