String is immutable. What exactly is the meaning? [duplicate]
问题 This question already has an answer here: Immutability of Strings in Java 26 answers I wrote the following code on immutable Strings. public class ImmutableStrings { public static void main(String[] args) { testmethod(); } private static void testmethod() { String a = \"a\"; System.out.println(\"a 1-->\" + a); a = \"ty\"; System.out.println(\"a 2-->\" + a); } } Output: a 1-->a a 2-->ty Here the value of variable a has been changed (while many say that contents of the immutable objects cannot