Difference between a string and an array of characters

前端 未结 7 2045
一整个雨季
一整个雨季 2021-02-01 10:18

How are these declarations different from each other?

String s=\"MY PROFESSION\";
char c[]=\"MY PROFESSION\";

What about the memory allocation

7条回答
  •  梦如初夏
    2021-02-01 10:59

    If you see the docs,

         String str = "abc";
    
    is equivalent to:
    
         char data[] = {'a', 'b', 'c'};  //  not 'abc'
         String str = new String(data);
    

    More over String literals are very special in java

    String is backed by a character array internally.

提交回复
热议问题