In this example, there are two sets of chars defined. One set is a lowercase 'a' and an uppercase 'A'. The second is a lowercase 'b' and an uppercase 'B'. Not surprisingly, the lowercase and uppercase chars have different numeric codes. Of even greater interest is that the difference between the lowercase and uppercase values of both the 'a' set and the 'b' set are the same (the value 32). The way the code tables are set up, this ration is set for all of the lowercase and uppercase letters.

Although there are only a limited number of characters on the keyboard, there is a much richer set available to the programmer. For example, if you look at the code table you will find that the value for PI symbol is 227. You then can print this out programmatically, with the code in Listing 4.

The output from the code is shown in Figure 4. Notice that a double quote is displayed and that there is an extra line before the last line of output.

Figure 4

Strings

The Java primitives that you just covered are not objects. By definition, objects have attributes and behaviors, which the primitive data types don't have. Other than these primitives, everything else in Java is an object. Whereas a character represents a single Unicode value, you can treat a string as an object that contains a collection of characters.

In fact, to obtain the functionality of a string, the C Programming Language used a character array to contain the individual units of the string.

char string[100];

You would literally manipulate the string as a collection of characters.

C++ provided some extensions, specifically a string class (using the The C++ Standard Template Library (STL)) that effectively wrapped the functionality of the character array into a nice package.

#include <string>
using std::string;
string s = "abc def abc";

Java has a class called String that was part of the original Java specification. As I continue the discussion on wrappers, you can think of a String class as a wrapper. It wraps the functionality of the character array that I mentioned earlier in a class. You then can create objects from this String class.

String s = "abc";

This String class provides the attributes and various methods that provide certain string manipulation functionality. The String class effectively wraps most of the functionality that you would need to process a string. For example, Listing 6 shows how the String method toUpperCase() works to manipulate the String object created.

The output from the code is shown in Figure 5. In this example, there are actually two strings created, s1 and s2. The following code does not really change the original string; it uses s1 to create a new string using the toUpperCase()method.