Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

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

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

Tutorial Details:

Java's character and assorted string classes support text-processing
Java's character and assorted string classes support text-processing
By: By Jeff Friesen
Explore Character, String, StringBuffer, and StringTokenizer
ext 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:
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.
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 .
Character supplies three methods that compare Character objects for ordering or other purposes. The public int compareTo(Character anotherCharacter) method compares the contents of two Character s by subtracting anotherCharacter 's value field from the current Character 's value field. The integer result returns. If the result is zero, both objects are the same (based on the value field only). If the result is negative, the current Character 's value is numerically less than the anotherCharacter -referenced Character 's value . Finally, a positive result implies that the current Character 's value field is numerically greater than anotherCharacter 's value field. A second overloaded public int compareTo(Object o) method works the same as compareTo(Character anotherCharacter) (and returns the same result), but compares the current Character and the o -referenced object (which must be of type Character , or the method throws a ClassCastException object). compareTo(Object o) allows Java's Collections Framework to sort Character s according to natural order. (A future article will discuss that method, sorting, and natural order.) Finally, the public final boolean equals(Object o) method compares the contents of the value field in the current Character with the contents of the value field in o . A Boolean true value returns if o is of type Character and if both value fields contain the same contents. Otherwise, false returns. To see the compareTo(Character anotherCharacter) and equals(Object o) methods in action, examine the following code fragment:
Character c1 = new Character ('A');
Character c2 = new Character ('B');
Character c3 = new Character ('A');
System.out.println ("c1.compareTo (c2): " + c1.compareTo (c2));
System.out.println ("c1.equals (c2): " + c1.equals (c2));
System.out.println ("c1.equals (c3): " + c1.equals (c3));
System.out.println ("c1.compareTo (c2): " + c1.compareTo (c2)); outputs -1 because A is (numerically) less than B . System.out.println ("c1.equals (c2): " + c1.equals (c2)); outputs false because the Character s that c1 and c2 reference contain different characters ( A and B ). Finally, System.out.println ("c1.equals (c3): " + c1.equals (c3)); outputs true because, although c1 and c3 reference different Character s, both objects contain the same character ( A ).
Character-oriented utility methods
Character serves as a repository for character-oriented utility methods. Examples of those methods include:
public static boolean isDigit(char c) , which returns a Boolean true value if c 's character is a digit. Otherwise, false returns.
public static boolean isLetter(char c) , which returns a Boolean true value if c 's character is a letter. Otherwise, false returns.
public static boolean isUpperCase(char c) , which returns a Boolean true value if c 's character is an uppercase letter. Otherwise, false returns.
public static char toLowerCase(char c) , which returns the lowercase equivalent of c 's character if it is uppercase. Otherwise c 's character returns.
public static char toUpperCase(char c) , which returns the uppercase equivalent of c 's character if it is lowercase. Otherwise c 's character returns.
The following code fragment demonstrates those five methods:
System.out.println (Character.isDigit ('4')); // Output: true
System.out.println (Character.isLetter (';')); // Output: false
System.out.println (Character.isUpperCase ('X')); // Output: true
System.out.println (Character.toLowerCase ('B')); // Output: b
System.out.println (Character.toUpperCase ('a')); // Output: A
Another useful utility method is Character 's public static char forDigit(int digit, int radix) , which converts digit 's integer value to its character equivalent in the number system that radix specifies and returns the result. However, if digit identifies an integer less than zero or greater than or equal to radix 's value, forDigit(int digit, int radix) returns the null character (represented in source code as Unicode escape sequence '\u0000' ). Similarly, if radix identifies an integer less than Character 's MIN_RADIX constant or greater than Character 's MAX_RADIX constant, forDigit(int digit, int radix) returns the null character. The following code demonstrates that method:
for (int i = 0; i < 16; i++)
System.out.println (Character.forDigit (i, 16));
That fragment converts integer numbers 0 through 15 to their character equivalents in the hexadecimal number system and outputs those character equivalents (0 through f).
To complement the forDigit(int digit, int radix) method, Character provides the public static int digit(char c, int radix) method, which converts the c -specified character value in the radix -specified number system, to the value's integer equivalent and returns the result. If c contains a nondigit character for the specified number system or radix is not in the MIN_RADIX / MAX_RADIX range, digit(char c, int radix) returns -1 . The following code demonstrates that method:
char [] digits = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'x' };
for (int i = 0; i < digits.length; i++)
System.out.println (Character.digit (digits [i], 16));
The fragment above converts the digits array's digit characters to their integer equivalents and outputs the results. Apart from the last character, each character represents a hexadecimal digit. (Passing 16 as the radix argument informs digit(char c, int radix) that the number system is hexadecimal.) Because x does not represent a hexadecimal digit, digit(char c, int radix) outputs -1 when it encounters that character.
To demonstrate Character 's isDigit(char c) and isLetter(char c) methods, I've created a CA (character analysis) application that counts a text file's digits, letters, and other characters. In addition to printing those counts, CA calculates and prints each count's percentage of the total count. Listing 1 presents CA 's source code (don't worry about the file-reading logic: I'll explain FileInputStream and other file-related concepts in a future article):
Listing 1: CA.java
// CA.java
// Character Analysis
import java.io.*;
class CA
{
public static void main (String [] args)
{
int ch, ndigits = 0, nletters = 0, nother = 0;
if (args.length != 1)
{
System.err.println ("usage: java CA filename");
return;
}
FileInputStream fis = null;
try
{
fis = new FileInputStream (args [0]);
while ((ch = fis.read ()) != -1)
if (Character.isLetter ((char) ch))
nletters++;
else
if (Character.isDigit ((char) ch))
ndigits++;
else
nother++;
System.out.println ("num letters = " + nletters);
System.out.println ("num digits = " + ndigits);
System.out.println ("num other = " + nother + "\r\n");
int total = nletters + ndigits + nother;
System.out.println ("% letters = " +
(double) (100.0 * nletters / total));
System.out.println ("% digits = " +
(double)


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Java's character and assorted string classes support text-processing

View Tutorial:
Java's character and assorted string classes support text-processing

Related Tutorials:

How to drag and drop with Java 2 - JavaWorld - March 1999
How to drag and drop with Java 2 - JavaWorld - March 1999
 
Reading textual data: Fun with streams - JavaWorld - April 1999
Reading textual data: Fun with streams - JavaWorld - April 1999
 
StringBuffer versus String - JavaWorld March 2000
StringBuffer versus String - JavaWorld March 2000
 
Tweak your IO performance for faster runtime - JavaWorld November 2000
Tweak your IO performance for faster runtime - JavaWorld November 2000
 
Optimize a query on a Map - JavaWorld November 2000
Optimize a query on a Map - JavaWorld November 2000
 
Printing in Java, Part 2 - JavaWorld December 2000
Printing in Java, Part 2 - JavaWorld December 2000
 
Device programming with MIDP, Part 1 - JavaWorld January 2001
Device programming with MIDP, Part 1 - JavaWorld January 2001
 
Design for performance, Part 2: Reduce object creation - JavaWorld February 2001
Design for performance, Part 2: Reduce object creation - JavaWorld February 2001
 
Matchmaking with regular expressions - JavaWorld July 2001
Matchmaking with regular expressions - JavaWorld July 2001
 
XSLT blooms with Java
XSLT blooms with Java
 
JavaWorld article
JavaWorld article
 
Java's character and assorted string classes support text-processing
Java's character and assorted string classes support text-processing
 
Chart a new course with JFreeChart
Chart a new course with JFreeChart
 
Sort it out
Sort it out
 
Trustin Lee\'s String/Object Converter - Changes
TL-convert Trustin Lee's String/Object Converter provides a simple API to convert Java objects into strings and vice versa. It is developed to replace Jakarta Commons BeanUtils and Jakarta Commons Convert and to provide only String/Object converters.
 
alt.lang.jre: Take a shine to JRuby
JRuby combines the object-oriented strength of Smalltalk, the expressiveness of Perl, and the flexibility of the Java class libraries into a single, efficient rapid development framework for the Java platform. In this third installment in the alt.lang.jre
 
iCal4j
iCal4j Overview iCal4j is a Java library used to read and write iCalendar data streams as defined in RFC2445. Providing both a parser and an object model, iCal4j allows you to either modify existing iCalendar data or create new data models. Validation
 
Core Java Interview Questions!
Core Java Interview Questions! Core Java Interview Questions Question: What is transient variable? Answer: Transient variable can't be serialize. For example if a variable is declared as transient in a Serializable class and the class is written
 
Adding slash "\" character before quote "'" in a query
Adding slash "\" character before quote "'" in a query Adding slash " \ " character before quote " ' " in a query During the inserting the records in the database if user enters the phrases like "What ' s your name?", database gives the error due
 
Connecting to the Database Using JDBC and Pure Java driver
Connecting to the Database Using JDBC and Pure Java driver Connecting to the Database JDBC Driver In our search engine we are using MySQL database server and MM.MySQL Driver for connecting our application to the database. MM.MySQL Driver is
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.