![]() |
The immutability of strings
Quick question...
Just having read through stuff recently and trying to be a little more concious of efficiency in my programs I wasn't sure about this. I know that having the following :
String test = "James";Actually creates two strings, and it's better to use stringbuilder class to do this type of thing. However, does this :
String test = "James" + " is testing this";Create one or two strings? I would assume one, but you know what they say about assuming! |
Re: The immutability of strings
It creates 3. One ("James"), two (" is testing this"), and a third to store the result ("James is testing this")
Nursing the efficiency of String vs. StringBuilder is often not going to get you much benefit. Of course, there are loops that are vastly improved by the change, but often it's easier to just use String. |
Re: The immutability of strings
Quote:
|
Re: The immutability of strings
I guess someone had to say that :)
|
Re: The immutability of strings
Thanks for the info Jimbo, very useful, I had already played some with StringBuilder for the scrabble program just because having the number of string operations that I had (for 8!) was a HUGE overhead, changing to stringbuilder vastly improved performance.
|
Re: The immutability of strings
Quote:
|
| All times are GMT -5. The time now is 4:13 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC