String and Character Array in Java

前端 未结 6 1923
长情又很酷
长情又很酷 2020-12-31 16:12

I am a student who has just shifted from C++ to Java.

In Java what could be the main reason for defining separate data types for Strings and Char arrays? Wha

6条回答
  •  情话喂你
    2020-12-31 16:50

    String is immutable. Char array is not. A string is implemented with a char array underneath but every time you try to modify it (like with concatenation, replace etc.) it gives you a new String object.

    So, String behaves as a constant Char array but comes with certain syntactic sugar that also makes them very easier to use. For example, the addition + operator has been overloaded as a string concatenation operator as well.

提交回复
热议问题