Pros and cons of immutable strings

Some languages ​​(C # or Java) have immutable strings, while others (such as Ruby) have mutable strings. What are the reasons for these desgin options?

+5
source share
4 answers

One reason why immutable strings are good is because it makes Unicode support easier. Modern Unicode can no longer effectively integrate into a fixed-size data cell that kills the one-to-one correspondence between the string index and the memory address, which allows them to use mutable strings.


( ASCII EBCDIC...), , ( C).

Unicode , - 16 , Java String ( StringBuffer s). , Unicode 16 , .

Unicode , - 16 , , Basic Multilingual Plane - , . Unicode, (32-?) .

O (1) - , , N'th. : .

, . , , , . , , .


, . , ( ) O (N ^ 2), N ...

- StringBuffer ConcatBuffer, . , , () .

, , , . " rope" "", , , , , , , , !

, "" O (log N) , " " , O (1) .

+5

, Java, . Java ( , - -). , . , Java , , (.. , , , , , , , , , ).

, , . , , .

+2

, . 10 11, 10 11. , , .

+1

, (.. ), , .

, , .

Lua .


, ( Common Lisp) , , - , (Python).

Lisp:

, ? : . - . , . , , ( ) ( ).


, JavaScript ( ) .

In the same vein, Clojure has transients that look like elegant pure functions on immutable data structures, but internally use mutable state for efficiency.

+1
source

All Articles