Why are strings immutable in Java?
Anónimo
"Well, they kinda just are. I wouldn't exactly be able to tell you why they designed it that way, but I do know that strings are normally stored in the string pool and strings are really just references to them. With that in mind, if a new string shares the same contents as another one, it will simply point to the existing string in the pool, which would allow == to return True if used to compare, but that is really just a reference equality check. Using == to check if strings are equal can lead to problems because if you allocate a string to the heap instead of the string pool by using new String(), the reference equality check would fail. You should always use .equals() to check equality, unless you're specifically wanting to check for reference equality."