If Strings cannot be modified, why (given declarations String s1, s2;) can we do s1 += s2 successfully?
Strings can’t be modified in the same sense as we did in C++ where they were treated as char arrays and we could go in and tinker with the individual characters. Instead, you think of strings as primitive entities like an integer or floating point number where you cannot change any piece of it. We can’t just change the exponent of a floating point number, for example. If we want to change a string, we build a new one and replace what was referenced. So the s1 += s2 works by creating a new string s1+s2 (+ performs an implicit new) and replaces the s1 reference to this new string containing the concatenation of the first two. The original string s1 used to point to is now a candidate for garbage collection. 2. I’m still a bit confused about ‘protected’ variables and functions. I’m gathering from the book that they’re somewhere between private and public in terms of security and that it has something to do with superclass and subclass access, but I’m still not exactly sure when to use the
Related Questions
- The string substitution abilities of the gLG.GetS method look pretty nice. How many substitution strings can be handled?
- Heres a neat trick for checking whether two strings are equal: if(!strcmp(s1, s2)) Is this good style?
- Did the Beatles use Pyramid flat wound strings on their six and twelve string guitars?