Immutable: String

The following are the some of the programmer friendly advantages of high level languages like Java, C# or Visual Basic by handling String as immutable as opposed to being mutable in languages like “C”

A) Efficient use of memory:

High level language make efficient use of memory by incorporating a special type of memory structure called "String Pool", each time any code creates a string literal, the Java Virtual Machine (Java) or Common language runtime checks to see if the String is already defined in the "StringPool" and if it exists, a reference is assigned as opposed to allocating space for every string.

For example:

If "ABCDEFGHIJKLMNOPQRSTUVWXYZ" is assigned10 times within a code, then in the StringPool, there will be a single copy of “ABCDEFGHIJKLMNOPQRSTUVWXYZ" and 10 references to it. This is a gain in the memory space allocation even though there is additional over head of maintaining “StringPool” memory structure.

In order for efficient memory utilization using "StringPool" to work, String has to be made immutable, otherwise, changing one references will change the rest of the references.
           
B) Thread safety:

When developing code for multiple threaded tasks, the challenge of synchronizing shared object or shared object's state across threads and synchronizing multiple threaded tasks for Write conflicts between one or more threads and Read-Write conflicts between one or more threads is not easy and it has to be done very carefully and correctly. From the programmer’s point of view, these kinds of programs are difficult to develop, test and debug. By making String immutable, the String variables are automatically made thread safe and can be shared by multiple threads without worrying about synchronizing issues between the threads, this greatly reduces the complexities associated in developing
Concurrent programming and it also reduces the overhead needed to run those kinds of programs gaining on the performance side.

C) Cache friendly:

When a "String" is cached, it is a guarantee that the cache will never get outdated because the reference will always have the same value later, however if a String is made mutable and when cached, there is always a risk of not keep the information update because the value itself could have potentially changed by some other thread or code. By keep String immutable, it enables one to be String data type cache friendly without the need of additional programming to ensure that the Cache is up-to-date.

D) Best Keys for Hash based collections:

Since String are immutable, they are the best candidates for key based collection such as HashMap and HashSet because, it is a 100% guarantee, the hash code of a String will always remain the same and the hash code itself need to be recalculated making Hash based collection implementation much faster.

E) Protection against modifications:

When a String is passed as a parameter to a function, because of the property of the Immutability, the calling function cannot modify the parent String reference unless explicitly documented, this in a way protects against accidentally changes which is good programming methodology; because in programming like C, the chances of a programmer making a mistake in affecting character array passed as a parameter is very high.  











Comments

Popular posts from this blog

Create SAS Data Sets w/o SAS

ACID vs BASE