My textbook says a String
is not over-writable or immutable, i.e, once you enter the value of a String you can't change it. But today when I was running the following code, the String str
gets muted as the compiler does not give any error and the new String a
's value is successfully entered into str
.
class Test
{
static void main()
{
String str = "something";
String a ="anything";
str = a; //str is being over written without any error
System.out.println(str);
}
}
The output is : anything
So, is my book wrong ?
If my book is not wrong please give an example to show that String
s are immutable