Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

If Strings cannot be modified, why (given declarations String s1, s2;) can we do s1 += s2 successfully?

0
Posted

If Strings cannot be modified, why (given declarations String s1, s2;) can we do s1 += s2 successfully?

0

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

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.