Open twisst opened 3 days ago
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!
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