JAVA基礎--JAVA API常見對象11

來源:互聯網
上載者:User

標籤:offset   image   示範   它的   bytes   目標   style   使用   天天向上   

一、 String 類型  1. String類引入

 

    第二天學習過Java中的常量:

 

               常量的分類:

 

                       數值型常量:整數,小數(浮點數)

 

                       字元型常量:使用單引號引用的資料

 

                       字串常量:使用雙引號引用的資料

 

                       布爾型常量:true或false

 

                       null常量:null

 

             上述的大部分的常量都可以使用Java中提供的8種基本類型進行開闢空間,儲存這些常量。

 

             字串常量不屬於8種基本類型中的資料。而它屬於Java中的參考型別資料(類類型資料)。

 

        字串資料在Java中使用String類進行描述。

 

        字串資料它屬於Java中一類資料,我們也可以把其看作一類事物。既然是一類事物,就一定可以使用Java中的某個類對其進行封裝和描述。

 

        後期我們學習的目標和重點:

 

                 不再是如何去描述一個類(不是去定義類)。而是要學習怎麼去使用別人(JDK)中提供好的類。需要知道這些類是幹什麼的,以及它中有哪些常用的方法,這些方法能夠解決什麼問題。

 

                   後期學習新的類的時候,學習方式:

 

                           重點是使用自己的代碼去測試這些類中的方法,並得到自己的結論。

    2.String 類介紹

           

      String 類代表字串。Java 程式中的所有字串字面值(如 "abc" )都作為此類的執行個體實現。

      在Java中只要使用雙引號引用起來的任何資料它們都是String 的一個對象。

        

      字串是常量;它們的值在建立之後不能更改。因為 String 對象是不可變的,所以可以共用。

      雖然使用雙引號引用起來的資料都是字串,也是String的一個執行個體對象,但是這個對象中的字元資料一定寫完,就已經是一個固定的資料,不能再進行修改,並且它儲存在方法區的字串常量池中。

      

      

    3.String類構造方法     

    String類中提供了大量的構造方法,都可以將不同形式的資料轉成字串對象。

    

 1 /* 2  * 示範String類的構造方法 3  */ 4 public class StringConstructorDemo { 5     public static void main(String[] args) { 6          7         method5(); 8     } 9     /*10      * 面試題11      */12     public static void method5() {13         14         String s = "abc";15         String s2 = new String("abc");16         /*17          * s 和 s2 的區別?18          * s它指向的方法區中的常量池中的 abc字串19          * s2它指向的是堆中的自己new的那個對象,而在堆中這個對象中維護的地址20          * 指向的常量池中的abc字串21          * 22          * String s = "abc"; 在記憶體中只有一個對象23          * String s2 = new String("abc"); 在記憶體中有2個對象24          */25         System.out.println(s);26         System.out.println(s2);27         System.out.println(s == s2);28     }29     /*30      * 示範將int類型的數組轉成字串31      */32     public static void method4() {33         34         int[] arr = {23456,23457,23458};35         String s = new String(arr , 0 , 3);36         System.out.println(s);37     }38     /*39      * String(byte[] bytes, int offset, int length) 40      *   byte[] bytes : 指定需要被轉成字串的那個位元組數組41      *   int offset : 從位元組數組的那個下標開始轉資料42      *   int length : 需要將從offset開始往後共計轉多少資料43      */44     public static void method3() {45         46         byte[] b = {97,98,99,100,65,49};47         48         String s = new String( b , 4 ,4 );49         System.out.println(s);50     }51     /*52      * 示範String類中的有參數的構造方法53      */54     public static void method2() {55         56         //建立位元組數組57         byte[] b = {97,98,99,100,65,49};58         59         //將位元組數組中的所有資料轉成字串60         String s = new String( b );61         62         System.out.println(s); 63     }64     /*65      *  示範空參數的構造方法66      *  String() 67      */68     public static void method1() {69         70         /*71          * 使用String類的構造方法得到String對象72          * 使用空參數的構造方法建立出的String對象,這個對象在堆中73          * 而這個字串對象中沒有任何字元資料74          */75         String s = new String();76         77     }78 }
    4. String 類常用方法           a. 拼接字串

               猜測到某個功能之後,繼續猜測它的方法參數以及傳回值類型

               參數:有,至少2。實際值需要一個參數

               傳回值類型:String , 拼接後的新的字串

      

      

      b.根據字元找下標

      

      

      

      

    c.根據下標找字元

      

    d.字串的長度

      

      

      e. 擷取字串中的一段子串

        

     

 1 /* 2  * 示範String類中的截取方法 3  */ 4 public class StringMethodDemo2 { 5     public static void main(String[] args) { 6          7         String s = "好好學習,天天向上"; 8         /* 9          *  substring(int beginIndex) 10          *      從指定的字串中的beginIndex位置開始截取子串,一直到結束11          *  substring( int beginIndex , int endIndex )12          *      從指定的字串中的beginIndex位置開始到endIndex結束(不包含endIndex位置上的字元)13          */14         String ss = s.substring(2);15         System.out.println(ss);16         String ss2 = s.substring(0, 5);17         System.out.println(ss2);18     }19 }
    f. 字串轉數組

      

      

      

 1 //字串轉成位元組數組 2     public static void method2() { 3         String s = "abdefg"; 4         //轉位元組數組 5         byte[] bs = s.getBytes(); 6          7         for (int i = 0; i < bs.length; i++) { 8             System.out.println(bs[i]); 9         }10     }11     //字串轉字元數組12     public static void method1() {13         String s = "abcefg";14         //轉字元數組15         char[] cs = s.toCharArray();16         for (int i = 0; i < cs.length; i++) {17             System.out.println(cs[i]);18         }19     }

 

    g. 其他判斷方法

 

 

 

 

 

 

 

 

 

 

 

 

 

    

JAVA基礎--JAVA API常見對象11

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.