Section 2.8
Details of Strings

JAVA HAS A BUILT-IN TYPE, String, to represent strings of characters. This type differs from the eight primitive types because a value of type String is an object. A String is not just a data value; it also has "methods." (A method is a subroutine that is part of an object.) For example, if str is a variable of type String, you can call the method str.length(), which is a function that returns the number of characters in the string. There's a lot more you can do with strings. This section covers some of the details.

One thing that you cannot do with strings is to use the relational operators <, >, <=, and <= to compare them. You can legally use == and != to compare Strings, but because of peculiarities in the way objects behave, they won't give the results you want. (I'll get back to this in a later chapter.) Instead, you should use methods that are provided for comparing Strings.

The String class defines a lot of methods. Here are just a few that you might find useful. Assume that s1 and s2 are Strings:

For the methods s1.toUpperCase(), s1.toLowerCase() and s1.trim(), note that the value of s1 is not changed. Instead a new string is created and returned as the value of the function. The returned value could be used, for example, in an assignment statement such as "s2 = s1.toLowerCase();".

Also useful is the fact that you can use the operator + to concatenate two strings. The concatenation of two strings is a new string consisting of all the characters of the first string followed by all the characters of the second string. For example, "Hello" + "World" evaluates to "HelloWorld". (Gotta watch those spaces, of course.)

Even more surprising is that you can concatenate values belonging to one of the primitive types onto a String using the + operator. The value of primitive type is converted to a string, just as it would be if you printed it to the console. For example, the expression "Number" + 42 evaluates to the string "Number42". And the statements

        console.put("After ");
        console.put(" years, the value is ");

can be replaced by the single statement:

        console.put("After " + years + 
                            " years, the value is " + principal);

Obviously, this is very convenient. You can even use the += operator to add something onto the end of a String. For example, if you want to read a sequence of letters from the console and store them in a String, you could do it like this:

             String str = "";              // start with an empty string
             char ch = console.getChar();  // read one character
             while ( (ch >= 'a' && ch <= 'z')
                       || (ch >= 'A' && ch <= 'Z') ) {
                // ch is a letter, so append it to the string
                //      and read the next character.
                str += ch;   // (note: same as  str = str + ch;)
                ch = console.getChar();
             } // end of while loop

End of Chapter 2

[ Next Chapter | Previous Section | Chapter Index | Main Index ]