Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Help Needed 1

Status
Not open for further replies.

dilse

Programmer
Jul 21, 2001
6
0
0
IN
How to insert characters one by one to a string using subscripts ?Please help me also for declaring such type of string.
 
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.

You can however do the following with Strings:

String myString = null;

myString = myString + 'A';
myString = myString + 'B';
myString = myString + 'C';

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.

Hope this make sense.
Brian
 
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 ----> visit me for Java and Data Warehousing resources
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top