class Character

class Character


Introduction

The java.lang.Character class includes many useful static methods with which text (or characters) can be manipulated. The other text manipulating classes are String, StringBuffer, StringTokenizer and StringBuilder.

The Character class is a wrapper class for char data type. That is, a Character object represents char as an object; it gives an object form to a simple data type char.

The static methods of Character class are useful

  1. To know the character type like whether the character is of lowercase or uppercase, or digit etc.
  2. To convert the character from lowercase to uppercase etc.

Following is the class signature as defined in Java API.

public final class Character extends Object implements Serializable, Comparable

As you can observe, the Character class is final. It cannot be extended (like Math and String etc).

Following program illustrates the usage of some methods of class Character.


class Character
Output screenshot on different methods of class Character

charValue() method returns the char stored in xchar object. compareTo() method returns an integer value of zero if both Character objects represent the same character, else non-zero value is returned. equals() method also compares two characters, but it returns a boolean value and is useful in control structures.

isJavaIdentifierStart(c2) checks whether P is suitable as Java identifier starting character. isJavaIdentifierPart(c3) checks whether the digit 8 is suitable to be placed anywhere in the identifier except as the starting character. isDefined(c2) returns true as P is defined in Java Unicode. toUpperCase() and toLowerCase() methods convert the character to uppercase from lowercase or vice versa. Other methods are self explanatory. Always in Java, the isXXX() methods return a boolean value.

The following program validates a password for non-presence of digits.

class Character

The password can be taken from keyboard or from command-line argument (it is taken directly to make the program simple). charAt() method of String class returns the character at the specified index (passed as parameter) in the string. isDigit() method checks the character is a digit or not.

File Analysis – Count

Let us write another program to practice the Character class methods. It is to count the number of uppercase letters, lowercase letters and digits present in a file and also to print all the uppercase letters.

Contents of text file Bench.txt

1. Do not waste time while you are on bench.
2. Learn new tools and new concepts.
3. Later, you may not get time to do the same.

The following program reads the above contents and prints the statistics.

class Character

The program requires the understanding of file copying using FileReader. The read() of FileReader reads each character in the file and returns it as an integer value.

char c1 = (char) temp;

The int data type returned by the read() method is converted back into character to be used in the code.

isLowerCase(c1) and isUpperCase(c1) checks whether the character c1 is lowercase or uppercase.

StringBuffer buffer1 = new StringBuffer();
buffer1.append(c1 + " ");

To store the uppercase letters temporarily, a StringBuffer object buffer1 is created. append() method of StringBuffer is used to add all the uppercase letters to preserve.

Leave a Reply

Your email address will not be published. Required fields are marked *