Owl Sams Teach Yourself Java 2 in 24 Hours

Hour 6: Using Strings to Communicate

NOTES AND CORRECTIONS

  • None

SOURCE FILES

READER QUESTIONS

  • Question: I can't seem to get the toUpperCase() method to change a string so that it's all capital letters. What am I doing wrong?
    Answer: The key to using toUpperCase() is to realize that it doesn't actually change the String object it is called on. Instead, it creates a new string that is all uppercase letters. Consider the following statements:
    • String firstName = "Abner";
    • String changeName = firstName.toUpperCase();
    • System.out.println("First Name: " + firstName);

    This example will output the text "First Name: Abner", because firstName contains the original string, while changeName contains it in capitalized form.

ACTIVITIES