Thursday, December 1, 2011

Java's character and assorted string classes support text-processing - 1


Explore Character, String, StringBuffer, and StringTokenizer
Text can represent a combination of digits, letters, punctuation, words, sentences, and more. Computer programs that process text need assistance (from their associated languages) to represent and manipulate text. Java provides such assistance through the Character, String, StringBuffer, and StringTokenizer classes. In this article, you'll create objects from these classes and examine their various methods. You'll also receive answers to three mysteries: why Java regards a string literal as a String object, why String objects are immutable (and how immutability relates to string internment), and what happens behind the scenes when the string concatenation operator concatenates two strings into a single string.
Note
Future articles will cover the Character, String, StringBuffer, and StringTokenizer methods that I omit in this discussion.

The Character class
Though Java already has a character type and char keyword to represent and manipulate characters, the language also requires a Character class for two reasons:
  1. Many data structure classes require their data structure objects to store other objects—not primitive type variables. Because directly storing a char variable in these objects proves impossible, that variable's value must wrap inside a Character object, which subsequently stores in a data structure object.
  2. Java needs a class to store various character-oriented utility methods—static methods that perform useful tasks and do not require Character objects; for example, a method that converts an arbitrary character argument representing a lowercase letter to another character representing the uppercase equivalent.

Character objects
The java.lang.Character class declares a private value field of character type. A character stores in value when code creates a Character object via class Character's public Character(char c) constructor, as the following code fragment demonstrates:
Character c = new Character ('A');

The constructor stores the character that 'A' represents in the value field of a new Character object that c references. Because the Character object wraps itself around the character, Character is a wrapper class.
By calling Character's public char charValue() method, code extricates the character from the Character object. Furthermore, by calling Character's public String toString() method, code returns the character as a String object. The following code, which builds on the previous fragment, demonstrates both method calls:
System.out.println (c.charValue ());
String s = c.toString ();

System.out.println (c.charValue ()); returns value's contents and outputs those contents (A) to the standard output device. String s = c.toString (); creates a String object containing value's contents, returns the String's reference, and assigns that reference to String variable s.

No comments: