Strings

We have seen how variables of the primitive data type char allow us to store and use individual letters and other characters, but more often than not the programs we will want to write will require storing more than just one character. For example, we might need to store someone's name, or a website address, or song lyrics, etc.

A String is a special non-primitive type that stores data in the form of text (i.e., a "string" of characters).

Strings can be either constructed in a manner consistent with working with other objects, as shown below:

String myString = new String("Hello World");

...or strings may be declared and initialized using the shortcut:

String myString = "Hello World";

This is not typical of most other object types.

There are many useful features of the String class that we could discuss. We will discuss most of these in detail at a later point in time -- but for now, let us confine our attention to just three things:



† Only the String class and a handful of other classes (e.g., the wrapper classes for numerical types: Integer, Double, etc.) can be added using the "+" operator. For most non-primitive data types where addition might make some sense, such a sum can only be found via a method call.