Strings

We have the char primitive type, and we have arrays, so if we need to include text as part of our game-state model—which we would if we wanted to model Scrabble, crossword puzzles, or Hangman—then we have everything we need. We would a model piece of text, such as the clue in a crossword puzzle, as char[].

But text is a particularly important data type in computer programming, and Java provides built-in support for it in the form of the String class. Inside the String class, the text is indeed modeled using a char[]. But around that representation, the String class provides support for common functions such as converting to upper-case, converting to lower-case, extracting sub-strings, and many others.

A fact about Strings to keep in mind is that they are immutable. Once a String is created, it cannot be modified. You can't change letters, add letters, or remove letters. So in a game-state model for a crossword puzzle, you would model a clue as a String, but you would model the state of the puzzle as (perhaps) a two-dimensional array of the char type. Playing a crossword puzzle involves repeatedly modifying the state of the puzzle so you could not use a type that cannot be modified.

At this point we're only making you aware of the String class as a built-in data type; we'll touch on the class in upcoming sections.