Is “new String()” immutable as well?

后端 未结 15 2515
日久生厌
日久生厌 2020-12-12 23:43

I\'ve been studying Java String for a while. The following questions are based on the below posts

Java String is special
Immutability of String in java

15条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-12 23:58

    From the Java Oracle documentation:

    Strings are constant; their values cannot be changed after they are created.

    And again:

    String buffers support mutable strings. Because String objects are immutable they can be shared.

    Generally speaking: "all primitive" (or related) object are immutable (please, accept my lack of formalism).

    Related post on Stack Overflow:

    • Immutability of Strings in Java
    • Is Java String immutable?
    • Is Java "pass-by-reference" or "pass-by-value"?
    • Is a Java string really immutable?
    • String is immutable. What exactly is the meaning?
    • what the reason behind making string immutable in java?

    About the object pool: object pool is a java optimization which is NOT related to immutable as well.

提交回复
热议问题