Introducing Strings

   

String handling in C or C++ (the languages that inspired Java) is infamously clunky . Java solves that problem the same way many C++ programmers do: by creating a String class. Java's String class enables your programs to manage text strings effortlessly, using statements similar to those used in simpler languages, such as BASIC or Pascal.

You can probably find many instances of the String class in most Java applications, especially business type applications. They are used extensively. So, what exactly is a Java String class anyway? In its simplest form, a string is nothing more than one or more text characters arranged consecutively in memory. You can think of a string as an array of characters , with this array having an index that starts at zero. (That is, the first character in the string is at array index 0.)

Note

The beginning index of a Java String is 0, not 1. This is very important and will make it very hard to find bugs if you are not aware of it and use 1 as the beginning index of a string. Many things in Java start with 0 instead of 1. This is just one more.


Troubleshooting Tip

If you are having trouble with String indexes, see "One Off Index Error," in the "Troubleshooting" section at the end of this chapter.


Unfortunately, few computer languages deal with strings in such a simple form. This is because a program needs to know where a string ends, and there are several different solutions to the length problem. Pascal, for example, tacks the length of the string onto the front of the characters, whereas C++ expects to find a null character (a zero) at the end of the string. A Java String is an object, rather thing just being a primitive or an array of primitives. This comes in handy, as you will see later.

In Java, strings can be represented by one of two classes:

  • String Best is used for string constants ”that is, for strings that are not going to change after they're created.

  • StringBuffer is used for strings that require a lot of manipulation, such as overriding a Class's toString method to provide a more meaningful display string for the class.

You'll learn more about the StringBuffer class later in this chapter. For now, concentrate on the String class itself.

Note

With the String class, you can do operations such as find, compare, and concatenate characters, but you cannot insert new characters into the string or change the length of the string. (Concatenating might seem like it is simply inserting new characters; however, in actuality it is creating an entirely new string.) This inability to modify an existing string is known as immutability .


Within an object of the String class, Java creates an array of characters much like that used for strings in C++ programs. Because this character array is hidden within the class, however, it cannot be accessed except through the class's methods . This data encapsulation (a key feature of object-oriented programming, by the way) ensures that the string will be maintained properly and will be manipulated in accordance with the rules of the class. You work with the String through the public methods of the class and can't modify the internal data structure.

The alternative to this approach would be to actually use an array of characters to represent a String. This is how several other languages handle Strings. The problem with that approach is that all code that modifies or works with the String must be careful not to misuse the String. The other problem is that the internal data structure of the String is exposed. That is, the client code using the array of characters is aware that the String is stored in an array. This is a violation of data encapsulation that is fundamental in object- oriented design and programming. In contrast, a Java String encapsulates how the actual String is stored. It could be stored in a different data structure, and because the client uses the public methods of the String class, the client does not need to know how it's stored internally. This is a good thing.

Figures 8.1 and 8.2 illustrate this concept. In Figure 8.1, a conventional C++ string is left hanging in memory where the program can manipulate it at will, regardless of whether the manipulation makes sense or results in a fatal error. In Figure 8.2, the string is protected by the methods of the class ”the only way the program can access the string.

Figure 8.1. In conventional programs, strings can be accessed directly by the program, leading to complications and errors.

graphics/08fig01.gif

Figure 8.2. By using a String class, the string can be accessed only through the class's methods, which eliminates many potential errors.

graphics/08fig02.gif

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net