String and Character Array in Java

前端 未结 6 1933
长情又很酷
长情又很酷 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:42

    There is a semantic difference. Just because data is stored the same way, this doesn't mean it's the same thing. Dates and Amounts may also have the same internal representation (long for a timestamp or fixed point amount of cash), but they're not the same. The char array could as well mean a 16-bit image.

    In object orientation, it's good practice to model objects based on what they are and can, and not by how they internally store their data. This allows you to encapsulate the data (and restrict or control (observer support) access with getters/setters, or even make the internal representation immutable or poolable), and provide appropriate methods for your objects.

提交回复
热议问题