Are Java String Objects an Array of Chars?

前端 未结 1 1535
猫巷女王i
猫巷女王i 2021-02-06 04:12

I am new to java and trying to understand the essentials and fundamentals of the language.

Is it accurate to state that Java string objects are intrinsicall

1条回答
  •  不知归路
    2021-02-06 04:42

    Is it accurate to state that Java string objects are intrinsically a class defined as an immutable array of chars?

    No. A Java String object is (currently - it's an implementation detail which I gather may be changing) a class containing a few fields:

    • A char[] containing the actual characters
    • A starting index into the array
    • A length
    • A cached hash code, lazily computed

    The reason for the index and length is that several strings can contain references to the same char[]. This is used by some operations such as substring (in many implementations, anyway).

    The important thing is the API for String though - which is very different to the API for an array. It's the API you would think of when you take the JLS definition into account: a String represents a sequence of Unicode code points. So you can take a subsequence (Substring), find a given subsequence (indexOf), convert it to an upper case sequence etc.

    In fact the JLS would be slightly more accurate to call it a sequence of UTF-16 code units; it's entirely possible to construct a string which isn't a valid sequence of Unicode code points, e.g. by including one half of a "surrogate pair" of UTF-16 code units but not the other. There are parts of the API which do deal with the String in terms of code units, but frankly most developers spend most of the time treating strings as if non-BMP characters didn't exist.

    0 讨论(0)
提交回复
热议问题