The Java Course provides a general introduction to programming in Java. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.


Strings Are Immutable

As you look over the documentation of the String methods, you might notice toUpperCase and toLowerCase. These methods are often a source of confusion, because it sounds like they have the effect of changing (or mutating) an existing string. Actually, neither these methods nor any others can change a string, because strings are immutable.

When you invoke toUpperCase on a String, you get a new String as a return value. For example:

    String name = "Alan Turing";
    String upperName = name.toUpperCase ();

After the second line is executed, upperName contains the value "ALAN TURING", but name still contains "Alan Turing".



Last Update: 2011-01-24