Skip to content

Commit b7fd5e8

Browse files
authored
Fixes for clarity and broken link in 'Strings and Drawing Text' #576
1 parent 7d7b7d1 commit b7fd5e8

File tree

1 file changed

+14
-4
lines changed
  • content/tutorials/text/strings-and-drawing-text

1 file changed

+14
-4
lines changed

content/tutorials/text/strings-and-drawing-text/index.mdx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,23 @@ println(message.length());
6363
We can also change a String to all uppercase using the [toUpperCase()](http://processing.org/reference/String_toUpperCase_.html) method ([toLowerCase()](http://processing.org/reference/String_toLowerCase_.html) is also available).
6464

6565
```
66-
message = message.toUpperCase();
67-
println(message);
66+
String uppercase = message.toUpperCase();
67+
println(uppercase);
6868
```
6969

70-
You might notice something a bit odd here. Why didn't we simply say `message.toUpperCase()` and then print `message` variable? Instead, we set the variable `message` to be the result of `message.toUpperCase()`.
70+
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`.
71+
72+
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.
73+
74+
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.
75+
76+
Note that it is possible to use the existing variable `message` to store this new String:
77+
78+
```
79+
message = message.toUpperCase();
80+
```
7181

72-
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()` cannot modify the original String in `message`—it only returns a copy of the String object with all caps. We can however use the existing variable `message` to store the new String object.
82+
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.
7383

7484
Finally, let's look at [equals()](http://processing.org/reference/String_equals_.html). Now, Strings can be compared with the `==` operator as follows:
7585

0 commit comments

Comments
 (0)