What you are trying to do can not be done with Strings because their data physically can not be changed in Java. If you want to change the data at specific subscripts, you must use class StringBuffer.
Understand though that each statement creates another myString reference to the String data each time it increases. myString references, let say 'A' after first line executes. Then the second line will have myString reference "AB". There is no reference to 'A' anymore so eventually the garbage collector will release its resource.
What BZJavaInst says about Strings is right, what leads on from what he says is....
....that if you only want to add to Strings a few times then you won't be creating too many new objects. However, don't go doing it loads of times (like in a loop) or you'll cause out-of-memory errors as the garbage collector won't kick in soon enough. Use a StringBuffer in that case.
Strings are a special type of object. My home ---->
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.