Strings no longer immutable?
Opened this issue · 5 comments
Select the type of content error.
Factual Mistake
Section
Tutorials
Location of the error
https://processing.org/tutorials/text
Describe the error
Are Strings in Processing still immutable or has that changed? I am by no means an expert Java programmer, but the section by Dan on Strings being immutable (dated 2008) seems to be no longer true. I can do this no problem:
String message = "bleep bloop";
message = message.toUpperCase();
println(message); // BLEEP BLOOP
Suggested correction
Simply replace the paragraphs below by:
The method toUpperCase() returns a copy of the String object with all caps. If you want the original String to be changed to all-caps, then assign the result to message:
String message = "bleep bloop"; message = message.toUpperCase(); println(message); // BLEEP BLOOP
Language
English
Screenshots or references
Current paragraphs:
"You might notice something a bit odd here. Why didn't we simply say message.toUpperCase() and then print message variable? Instead, we assigned the result of message.toUpperCase() to a new variable with a different name—uppercase.
This is because a String is a special kind of object. It is immutable. An immutable object is one whose data can never be changed. Once we create a String, it stays the same for life. Anytime we want to change the String, we have to create a new one. So in the case of converting to uppercase, the method toUpperCase() returns a copy of the String object with all caps."
Additional context
No response
Hi @twisst and thank you for bringing this up! You're correct that the example works because the result of toUpperCase()
is reassigned to the variable message
, but this doesn't change the fact that String
objects in Java are immutable.
The sentence in the tutorial:
Why didn't we simply say
message.toUpperCase()
and then print themessage
variable?
aims to clarify a potential misunderstanding: .toUpperCase()
does not modify the original message
directly but instead returns a new String
object.
That said, the explanation could be updated to make this behavior clearer and avoid confusion.
If you're interested, you can propose a change by creating a pull request to update the tutorial with your suggestion. If you need help with submitting a PR or have questions about the process, feel free to reach out!
I am truly terrible with Github, but I think I did it :-)
Thanks for your PR. You're doing great! We're all learners here
To avoid introducing too many concepts at once, I'd suggest focusing solely on the immutability of String and how toUpperCase()
works, leaving the first part unchanged but adding a part about re-assignment to address the potential confusion.
What would you think of something like this?
Suggested Update
We can also change a String to all uppercase using the toUpperCase() method (toLowerCase() is also available).
String uppercase = message.toUpperCase();
println(uppercase);
You might notice something a bit odd here. Why didn't we simply say message.toUpperCase()
and then print message
variable? Instead, we assigned the result of message.toUpperCase()
to a new variable with a different name—uppercase
.
This is because a String is a special kind of object. It is immutable. An immutable object is one whose data can never be changed. Once we create a String, it stays the same for life. Anytime we want to change the String, we have to assign it to a variable.
So, in the case of converting to uppercase, the method toUpperCase()
cannot modify the original String in message
. It only returns a new copy of the String object with all caps.
Note that it is possible to use the existing variable message
to store this new String:
message = message.toUpperCase();
You might be thinking, "Wait, didn’t you just say Strings are immutable?" Yes, that’s true. The code above doesn’t change the original String. Instead, it updates the message
variable to point to the new String object created by toUpperCase()
. Without this reassignment (using the =
operator), the original message
would remain unchanged.
Much better!
@twisst Great! Could you update your pull request to include these changes? Alternatively, you could allow maintainers to make changes to your pull request and I can make the changes myself.