Since String is Immutable How can we will change String for Example
String s1="Hello";
String s2="Manas";
Now I am making
s1=s1+s3;
It will Print "Hello Manas". So String s1 will change.
So how can u say that String is immutable?
Since String is Immutable How can we will change String for Example
String s1="Hello";
String s2="Manas";
Now I am making
s1=s1+s3;
It will Print "Hello Manas". So String s1 will change.
So how can u say that String is immutable?
You're not changing the contents of "Hello"
, you're changing the value of the variable s1
to refer to a different string object entirely.
Immutability has nothing to do with changing the value of the variable - if you want to prevent that, you can make the variable in question final.