0

I'm looking to better my understanding of Strings. The way they operate often seems unintuitive to me, and I'm wondering what the logic/ rationale is on why they work like they do. Some examples:

1) Immutability - Often, I would like to change an individual char of a String, but this requires either creating a new String, or converting the String to some other mutable object and converting it back. If String were mutable, this could be done much faster (both in terms of writing the code and executing it). Why are they immutable?

2) Concatenation - String concatenation is expensive, because it requires a brand-new String to be created, rather than modifying the current String. Why not allow a String to dynamically resize as ArrayLists do and skip this wasted time?

3) Typing (Java) - As I understand it, Strings are not a primitive type, but seem to behave like one. Why is this?

Tydal
  • 75
  • 4
  • [java why are strings immutable](https://www.google.com/search?q=java+why+are+strings+immutable&oq=java+why+are+strings+immutable&aqs=chrome..69i57.7111j0j7&sourceid=chrome&ie=UTF-8); Point 2 goes back to point 1 – MadProgrammer Jun 21 '22 at 23:41
  • 4
    You can use StringBuilder for mutability and concatenation. In what way does String behave like a primitive? – shmosel Jun 21 '22 at 23:42
  • 1
    Hi Tydal, [this video](https://youtu.be/Bj9Mx_Lx3q4) might help you to understand the reason for the immutability of strings in java. – Fernando Bravo Diaz Jun 21 '22 at 23:43
  • @shmosel You don't need to do "String s = new String("mytext");" as other objects would require. Why not allow a String to do what StringBuilder does naturally without the need for a 2nd data object type? – Tydal Jun 21 '22 at 23:49
  • `"mytext"` is already a String object; passing it to the constructor would be redundant. But I see your point that it's the only non-primitive type that has a literal syntax. For now. Unless you count boxed types. Or arrays. Or lambdas. – shmosel Jun 21 '22 at 23:57
  • 1
    I can see your point - that a mutable, cheaply conatenable String-like type might be useful. But that actually exists - there's `StringBuffer`, which has a level of thread-safety built in; and `StringBuilder`, which kind of doesn't, but is good for String-like processing that happens within a single method. But `String` is something different. It's what you use when you want something immutable, which in turn makes it awkward for doing concatenation. So start getting used to `StringBuffer` or `StringBuilder` whenever you need mutable String-like objects that are cheap to concatenate. – Dawood ibn Kareem Jun 22 '22 at 00:10
  • Oh, and I have no idea what your point (3) means. Behaving like a primitive type sounds like something my ex-wife would accuse me of. – Dawood ibn Kareem Jun 22 '22 at 00:10
  • If `String` could not be created via "string literals", then you'd have this kind of code all over the place: `String s = new String(new char[]{'H', 'e', 'l', 'l', 'o', '!'});`. Since strings are an extremely common and fundamental datatype, on the same scale as numbers and booleans, the Java language added the ability to create strings using string literals. But `String` is still a reference type, not a primitive. – Slaw Jun 22 '22 at 01:24

0 Answers0