+ 2
Why strings are immutable in java??
this is already known question but i want more clarity with examples...
3 ответов
+ 10
check it once,
https://code.sololearn.com/cRFSi8C88PYG/?ref=app
+ 2
it means that, once we create a string object, we can't perform any changes in the existing object, if we are trying to change a new object will create.
Is that same or what? so this feature is called immutable or what?
+ 1
Principally for caching and security reason.
For first imagine two string with same value, with string collector pool we need only one object for storing them. The second concern variable that value must can't change at runtime. Imagine a string to dB connection or network URL.
Another important things:
don't use -> new String("bad");
use this one -> String s = "well done";