Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 7th, 2008, 6:41 PM   #1
Arla
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 164
Rep Power: 4 Arla is on a distinguished road
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";
test += " is testing this";

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!
Arla is offline   Reply With Quote
Old Apr 7th, 2008, 11:16 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 754
Rep Power: 3 Jimbo is on a distinguished road
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.
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Old Apr 8th, 2008, 6:52 AM   #3
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
Re: The immutability of strings

Quote:
Originally Posted by Jimbo View Post
Nursing the efficiency of String vs. StringBuilder is often not going to get you much benefit.
..not to mention that if efficiency is critical to your application, you should use a native (non=managed) language like C/C++.
OpenLoop is offline   Reply With Quote
Old Apr 8th, 2008, 11:41 AM   #4
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 381
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Re: The immutability of strings

I guess someone had to say that
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Apr 8th, 2008, 1:56 PM   #5
Arla
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 164
Rep Power: 4 Arla is on a distinguished road
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.
Arla is offline   Reply With Quote
Old Apr 8th, 2008, 1:58 PM   #6
Arla
Hobbyist Programmer
 
Join Date: Mar 2005
Posts: 164
Rep Power: 4 Arla is on a distinguished road
Re: The immutability of strings

Quote:
Originally Posted by OpenLoop View Post
..not to mention that if efficiency is critical to your application, you should use a native (non=managed) language like C/C++.
Efficiency isn't critical, but I like to try to program in an efficient manner, taking 10 seconds to do something that should take less than a second (if programmed in the same language) is bad practice.
Arla is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Splitting strings into other strings Keiro C 2 Dec 11th, 2006 6:10 AM
inserting strings into other strings codylee270 C 2 Dec 3rd, 2005 9:37 PM
Question about multidimensional arrays of strings aznluvsmc C 8 Oct 15th, 2005 10:20 PM
creating Strings pleasehelpmeeee Java 5 Sep 26th, 2005 8:03 PM
pointers strings, and strcat() conbrio C 3 Apr 16th, 2005 1:23 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC