I have the following code, which I have written to check how string is immutable.
My code is:
class Check
{
public static void main(String k[])
{
String a1="JohnPlayer";
a1.concat("America");//line no 6
String a2="America";//line no 7
a1=a1+a2;//line no 8
System.out.println("Value of a1="+a1);
}
}
In line no 6, when I use concat
, it print only "JohnPlayer",
while If I concatenate a1 and a2 using a1+a2
, it prints the concatenated value "JohnPlayerAmerica" . In this case, how can I say String
is immutable?