https://www.quora.com/What-is-meant-by-immutable-I-can-declare-String-passWord-Passw1-and-then-in-the-following-line-overwrite-it-meaning-its-not-protected-or-final-or-immutable-by-stating-passWord-Passw2-Please-help-with-this-grey-area/answer/Alan-Mellor
What is meant by immutable? I can declare String passWord = 'Passw1' and then in the following line overwrite it (meaning it's not protected or final or immutable) by stating passWord = 'Passw2'. Please help with this grey area for me? - Quora
What is meant by immutable? I can declare String passWord = "Passw1" and then in the following line overwrite it (meaning it's not protected or final or immutable) by stating passWord = "Passw2". Please help with this grey area for me?
Yes it's a subtle one this.
The variable passWord is not a String! It is a kind of pointer to a String. So the first piece of text gets assigned to a String object that is immutable and passWord points to it. In Java, these are called references
Once you create the second immutable String object and assign that to the reference passWord, that first String object gets garbage collected (at some point)
Strings in Java live in memory as unchangable things and each time you change one, a new one gets made. The programmer can then change a reference to it
Yes it's a subtle one this.
The variable passWord is not a String! It is a kind of pointer to a String. So the first piece of text gets assigned to a String object that is immutable and passWord points to it. In Java, these are called references
Once you create the second immutable String object and assign that to the reference passWord, that first String object gets garbage collected (at some point)
Strings in Java live in memory as unchangable things and each time you change one, a new one gets made. The programmer can then change a reference to it
No comments:
Post a Comment