Many programming languages (such as Java) make strings immutable to mitigate many potential serious security threats.
Are there any major downsides or issues with a language having all strings immutable?
Programming Language Design and Implementation Stack Exchange is a question and answer site for designers and implementers of computer programming languages. It only takes a minute to sign up.
Sign up to join this communityMany programming languages (such as Java) make strings immutable to mitigate many potential serious security threats.
Are there any major downsides or issues with a language having all strings immutable?
Immutability in general is bad for performance. If you concatenate many strings together in a loop, for example, the needless copying and the GC overhead (if any) will hurt performance. For this reason, many languages have a mutable string type, for example Java’s StringBuilder
.
When memory is freed it is not cleared. The string may stay in memory for a long time after you are done using it potentially exposing sensitive information to other processes that can snoop on your memory.
For that reason, some languages have the concept of a SecureString
that is mutable so you can zero out the data when you are done using it to prevent other processes from reading it.
The only "definite" downside I can think of is C-FFI. If you want to have C-interop, you may need to convert your language into C-style string (which is mutable) back and forth every time, or you try to use strings as char arrays in your language directly to avoid the conversion, which mostly defeats the purpose of having a dedicate string type.