Case 1:
String str = "Good";
str = str + " Morning";
In the above code you create 3 String Objects.
- "Good" it goes into the String Pool.
- " Morning" it goes into the String Pool as well.
- "Good Morning" created by concatenating "Good" and " Morning". This guy goes on the Heap.
Note: Strings are always immutable. There is no, such thing as a mutable String. str is just a reference which eventually points to "Good Morning". You are actually, not working on 1 object. you have 3 distinct String Objects.
Case 2:
StringBuffer str = new StringBuffer("Good");
str.append(" Morning");
StringBuffer contains an array of characters. It is not same as a String.
The above code adds characters to the existing array. Effectively, StringBuffer is mutable, its String representation isn't.
Case 1:
String str = "Good";
str = str + " Morning";
In the above code you create 3 String Objects.
- "Good" it goes into the String Pool.
- " Morning" it goes into the String Pool as well.
- "Good Morning" created by concatenating "Good" and " Morning". This guy goes on the Heap.
Note: Strings are always immutable. There is no, such thing as a mutable String. str is just a reference which eventually points to "Good Morning". You are actually, not working on 1 object. you have 3 distinct String Objects.
Case 2:
StringBuffer str = new StringBuffer("Good");
str.append(" Morning");
StringBuffer contains an array of characters. It is not same as a String.
The above code adds characters to the existing array. Effectively, StringBuffer is mutable, its String representation isn't.
What is difference between mutable and immutable String in java
immutable exist, mutable don't.
Mutable or Immutable strings?
[Java] If strings are immutable why can their contents be changed?
Why String is immutable in Java
What does 'Strings are immutable' mean?
What's the difference between mutable and immutable objects?
Which Python types are immutable?
Why does mutability matter in interviews?
Videos
What are the advantages of representing strings with an immutable data strucure?
A few good points i've read:
-
Easier to compare (compare by pointer)
-
Less expensive to copy
-
String pool cuts down the use of memory
Other than that, most other arguments are about immutable objects in general, ie. thread safety, optimizations, easier to reason, etc
That said, i'm considering to represent strings internally with a Mutable data structure, essentially, the string type is an alias to []byte (dynamic size byte array or byte slice), and as such is completely mutable. And here is why:
-
Fits better with the type system (specially with generics1), since all operations on slices can be done with strings
-
Less confusion on why you can mutate an item in a slice but not on a string
-
Less runtime complexity
Additionally, since function parameters are immutable by default in my language, we can pass slices by reference, not needing a copy. If you need to mutate an argument, however, i'm still deciding, either you need to explicitly pass a pointer to the value you want to mutate or you must specify that particular parameter as mutable.
1 - There are 5 (and only 5) typeclasses in my language: anything -> comparable -> orderable -> numerical -> integer. Each typeclass has associated operators. User defined types are automatically assigned to this typeclasses if they can be ordered, compared, etc. The language has an 'append' operator: a .. b appends b to the end of a, this operation is supported by both strings and slices. If strings are not slices, then i would need yet another typeclass to represent appendables. With strings being aliases, it's trivial to write generic algorythms that work in both slices and strings:
generic ReverseInPlace over T
fn(list:&[]T) {
var start = 0,
end = (@list).length;
for start < end {
val temp <- (@list)[start];
(@list)[start] <- (@list)[end];
(@list)[end] <- temp;
start <- start + 1;
end <- end - 1;
}
}
Edit: Thank you for all your input, after careful consideration, i decided to issue a compiler warning whenever string is indexed or mutated in place. That way the user will still be aware of string encodings but the type will fit well within the type system, without creating a special case. string is an alias to []byte but with training wheels, since the compiler knows this particular set of bytes is talking about the mess that is the human language. I'm sorry if that's not the outcome you expected, but in my view it's what best fits the language.
Ok so I'm a bit confused by what it means to be immutable. I was under the impression that after the initialization of a String it cannot be changed. But why does code like this work:
String s = "a";
for(int i = 0; i < x; i++){
s += "other string";
}I'm just confused as to why this isn't giving any errors, since it is changing the value of the string.