Why Strings are immutable in Java?
The String is immutable in Java because of its security, thread safety, and memory utilization. Let's go through each reason one by one.
1. Memory space utilization
When we declare a string variable,
in memory it looks like this,
The name variabl...
shashankbhatgs.hashnode.dev2 min read
Shai Almog
Yak Barber, duck whisperer, hunter of bugs, feeder of dogs, heap climber, stack explorer and rider of shells
Great post!
Also I think the reason is to make it a bit more consistent with the behavior of the primitive types.
I would also add that the JVM can then do some pretty cool optimizations to Strings seamlessly thanks to that...