Vikas Rajput
Vikas Rajput

@vikasrajputin

8 تغريدة 1 قراءة Dec 06, 2022
Strings are Mostly Immutable in Many Programming Languages like (Java, Python, Javascript, C#, etc) ?
Meaning, you cannot change its value, once it's created.
Here's why it is designed this way:
Note: Let's understand this from Java's perspective:
1. String as Keys
Hashtable & Hashmap are widely using strings as keys to store information.
If strings are mutable, these keys can be changed easily and also change the internal mapping between key and value, making the whole data structure fail miserably.
2. Memory Optimization
When we create a String using literal, it is stored in the String pool.
Next time, when we create another String with the same value, it is referred to the previously created String hence saving the memory.
3. Thread Safety
String immutability enables threads to safely assume that string they're using will never going to be changed by any other thread making them ultimately thread-safe.
4. Less Error Prone
Imagine writing a code where we're running a sensitive business logic on username or password or some other sensitive string object, in such case we want to be 1000% sure that the string will never be going to be changed else it can lead to business failure.
That's a wrap!
I help people get started with Backend Development.
Follow me @vikasrajputin for more.
If you find this thread helpful then Like/Retweet the first tweet below:
Before you go, do you know?
I also write on LinkedIn!
I share some exclusive content there, which I never share here.
Follow me on Linkedin to never miss an important update on Backend content:
linkedin.com

جاري تحميل الاقتراحات...