標籤:有趣的 main false [] bsp color play 分享 one
1. 字串基本知識
用java.lang.String類的對象表示字串
字串類型不是基本類型, 而是參考型別(類似於數組和對象)
Java基於Unicode字元集
2. 字串建立
1 public static void main(String[] args) { 2 String s1 = new String(); 3 String s2 = new String("Hello"); 4 5 //錯誤的建立方式 6 //String s3 = new String(‘H‘,‘e‘,‘l‘,‘l‘,‘o‘); 7 char[] Arr = {‘H‘,‘e‘,‘l‘,‘l‘,‘o‘}; 8 //public String(char[] value) 9 String s3 = new String(Arr);10 11 //public String(char[] value, int offset, int count)12 //offset數組起始元素下標, count表示元素個數13 String s4 = new String(Arr,0,4);14 15 //特殊方式16 String s5="Hello";17 18 System.out.println("s1="+s1);19 System.out.println("s2="+s2);20 System.out.println("s3="+s3);21 System.out.println("s4="+s4);22 System.out.println("s5="+s5);23 }View Code
輸出結果:
3. 字串有趣的問題
1 String s1 = "Hello!";2 String s2 = "Hello!";3 System.out.println ( s1 == s2 );//輸出true4 5 String s1 = "Hello!";6 String s2 = new String("Hello!");7 System.out.println(s1 == s2);//輸出falseView Code
Java字串理解