Java字串理解

來源:互聯網
上載者:User

標籤:有趣的   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);//輸出false
View Code

 

Java字串理解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.