Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the advantage of a String being Immutable?

Tags:

Once I studied about the advantage of a string being immutable because of something to improve performace in memory.

Can anybody explain this to me? I can't find it on the Internet.

like image 982
Alan Avatar asked Aug 04 '10 15:08

Alan


People also ask

What are advantages of immutable string?

Being immutable automatically makes the String thread safe since they won't be changed when accessed from multiple threads. Hence immutable objects, in general, can be shared across multiple threads running simultaneously.

What is the main advantage of immutable objects?

The advantage of immutable objects is that you know their data cannot change, so you don't have to worry about that. You can pass them around freely without having to remember whether a method you pass them to could change them in a way your code is not prepared to handle. That makes working with immutable data easier.

What are the advantages & disadvantages of immutable?

An immutable object remains in exactly one state, the state in which it was created. Therefore, immutable object is thread-safe so there is no synchronization issue. They cannot be corrupted by multiple threads accessing them concurrently. This is far and away the easiest approach to achieving thread safety.

Why is string made immutable?

The String is immutable in Java because of the security, synchronization and concurrency, caching, and class loading. The reason of making string final is to destroy the immutability and to not allow others to extend it. The String objects are cached in the String pool, and it makes the String immutable.


2 Answers

Immutability (for strings or other types) can have numerous advantages:

  • It makes it easier to reason about the code, since you can make assumptions about variables and arguments that you can't otherwise make.
  • It simplifies multithreaded programming since reading from a type that cannot change is always safe to do concurrently.
  • It allows for a reduction of memory usage by allowing identical values to be combined together and referenced from multiple locations. Both Java and C# perform string interning to reduce the memory cost of literal strings embedded in code.
  • It simplifies the design and implementation of certain algorithms (such as those employing backtracking or value-space partitioning) because previously computed state can be reused later.
  • Immutability is a foundational principle in many functional programming languages - it allows code to be viewed as a series of transformations from one representation to another, rather than a sequence of mutations.

Immutable strings also help avoid the temptation of using strings as buffers. Many defects in C/C++ programs relate to buffer overrun problems resulting from using naked character arrays to compose or modify string values. Treating strings as a mutable types encourages using types better suited for buffer manipulation (see StringBuilder in .NET or Java).

like image 85
LBushkin Avatar answered Dec 14 '22 19:12

LBushkin


Consider the alternative. Java has no const qualifier. If String objects were mutable, then any method to which you pass a reference to a string could have the side-effect of modifying the string. Immutable strings eliminate the need for defensive copies, and reduce the risk of program error.

like image 43
Andy Thomas Avatar answered Dec 14 '22 20:12

Andy Thomas