-
Book Overview & Buying
-
Table Of Contents
Everyday data structures
By :
Strings are not precisely data types, although as developers we very often treat them as such. In actuality, strings are simply objects whose value is text; under the hood, strings contain a sequential collection of read-only char objects. This read-only nature of a string object makes strings immutable, which means the objects cannot be changed once they have been created in memory.
It is important to understand that changing any immutable object, not just a string, means your program is actually creating a new object in memory and discarding the old one. This is a more intensive operation than simply changing the value of an address in memory and requires more processing. Merging two strings together is called concatenation, and this is an even more costly procedure as you are disposing of two objects before creating a new one. If you find that you are editing your string values frequently, or frequently concatenating strings together, be aware that your program is not as efficient as it could be.
Strings are strictly immutable in C#, Java, and Objective-C. It is interesting to note that the Swift documentation refers to strings as mutable. However, the behavior is similar to Java, in that, when a string is modified, it gets copied on assignment to another object. Therefore, although the documentation says otherwise, strings are effectively immutable in Swift as well.
C#
C# uses the string keyword to declare string types:
//C#
string one = "One String";
Console.WriteLine("One: {0}", one);
String two = "Two String";
Console.WriteLine("Two: {0}", two);
String red = "Red String";
Console.WriteLine("Red: {0}", red);
String blue = "Blue String";
Console.WriteLine("Blue: {0}", blue);
String purple = red + blue;
Console.WriteLine("Concatenation: {0}", purple);
purple = "Purple String";
Console.WriteLine("Whoops! Mutation: {0}", purple);
Java
Java uses the system class String to declare string types:
//Java
String one = "One String";
System.out.println("One: " + one);
String two = "Two String";
System.out.println("Two: " + two);
String red = "Red String";
System.out.println("Red: " + red);
String blue = "Blue String";
System.out.println("Blue: " + blue);
String purple = red + blue;
System.out.println("Concatenation: " + purple);
purple = "Purple String";
System.out.println("Whoops! Mutation: " + purple);
Objective-CObjective-C provides the NSString class to create string objects:
//Objective-C
NSString *one = @"One String";
NSLog(@"One: %@", one);
NSString *two = @"Two String";
NSLog(@"Two: %@", two);
NSString *red = @"Red String";
NSLog(@"Red: %@", red);
NSString *blue = @"Blue String";
NSLog(@"Blue: %@", blue);
NSString *purple = [[NSArray arrayWithObjects:red, blue, nil] componentsJoinedByString:@""];
NSLog(@"Concatenation: %@", purple);
purple = @"Purple String";
NSLog(@"Whoops! Mutation: %@", purple);
When you examine the Objective-C example, you might wonder why we have all that extra code for creating the purple object. That code is necessary because Objective-C does not provide a shortcut mechanism for concatenating strings like the other three languages we're using. So, in this scenario, I have chosen to place the two strings into an array and then call the NSArray method componentsJoinedByString:. I could have also chosen to use the NSMutableString class, which provides a method for concatenating strings. However, since we're not discussing mutable string classes in any of our selected languages, I have opted not to use that approach.
Swift
Swift provides the String class to create string objects:
//Swift
var one : String = "One String"
print("One: \(one)")
var two : String = "Two String"
print("Two: \(two)")
var red : String = "Red String"
print("Red: \(red)")
var blue : String = "Blue String"
print("Blue: \(blue)")
var purple : String = red + blue
print("Concatenation: \(purple)")
purple = "Purple String";
print("Whoops! Mutation: \(purple)")
/*
Output from each string example:
One: One String
Two: Two String
Red: Red String
Blue: Blue String
Concatenation: Red StringBlue String
Whoops! Mutation: Purple String
*/