The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Object Basics and Simple Data Objects

Characters

An object of Character (in the API reference documentation) type contains a single character value. You use a Character object instead of a primitive char variable when an object is required—for example, when passing a character value into a method that changes the value or when placing a character value into a data structure, such as an ArrayList, that requires objects.


Note:  As of JDK 5.0, it is possible to pass a primitive data types (such as char) directly into methods expecting a wrapper objects (such as Character). This conversion happens for you automatically, and is known as autoboxing. Conversely, it is also possible to extract a primitive data type directly from a method returning a wrapper object. This automatic conversion is known as unboxing. The manual conversion code shown in this section is valid for any JDK release, but is only required for versions prior to 5.0.

The following sample program, CharacterDemo (in a .java source file), creates a few character objects and displays some information about them. The code that is related to the Character class is shown in boldface:

public class CharacterDemo {
    public static void main(String args[]) {
        Character a = new Character('a');
        Character a2 = new Character('a');
        Character b = new Character('b');

        int difference = a.compareTo(b);

        if (difference == 0) {
            System.out.println("a is equal to b.");
        } else if (difference < 0) {
            System.out.println("a is less than b.");
        } else if (difference > 0) {
            System.out.println("a is greater than b.");
        }
        System.out.println("a is "
                           + ((a.equals(a2)) ? "equal" : "not equal")
                           + " to a2.");
        System.out.println("The character " + a.toString() + " is "
            + (Character.isUpperCase(a.charValue()) ? "upper" : "lower")
            + "case.");
    }
}
The following is the output from this program:
a is less than b.
a is equal to a2.
The character a is lowercase.
In the above example, the code Character.isUpperCase(a.charValue()) extracts the char value from the Character object named a. This is because the isUpperCase method accepts a parameter of type char. If you are using JDK 5.0 or later, you can take advantage of unboxing by simply passing this method the Character object:
Character.isUpperCase(a)
The CharacterDemo program calls the following constructors and methods provided by the Character class:
Character(char)
The Character class's only constructor, which creates a Character object containing the value provided by the argument. Once a Character object has been created, the value it contains cannot be changed.

compareTo(Character)
An instance method that compares the values held by two character objects: the object on which the method is called (a in the example) and the argument to the method (b in the example). This method returns an integer indicating whether the value in the current object is greater than, equal to, or less than the value held by the argument. A letter is greater than another letter if its numeric value is greater.

equals(Object)
An instance method that compares the value held by the current object with the value held by another. This method returns true if the values held by both objects are equal.

toString()
An instance method that converts the object to a string. The resulting string is one character in length and contains the value held by the character object.

charValue()
An instance method that returns the value held by the character object as a primitive char value.

isUpperCase(char)
A class method that determines whether a primitive char value is uppercase. This is one of many Character class methods that inspect or manipulate character data.
The following table lists several other useful class methods the Character class provides, but is not meant to be exhaustive. For a complete listing of all methods in this class, refer to the java.lang.Character (in the API reference documentation) API specification.

Useful Class Methods in the Character Class
Method Description
boolean isUpperCase(char)
boolean isLowerCase(char)
Determines whether the specified primitive char value is upper- or lowercase, respectively.
char toUpperCase(char)
char toLowerCase(char)
Returns the upper- or lowercase form of the specified primitive char value.
boolean isLetter(char)
boolean isDigit(char)
boolean isLetterOrDigit(char)
Determines whether the specified primitive char value is a letter, a digit, or a letter or a digit, respectively.
boolean isWhitespace(char)a Determines whether the specified primitive char value is white space according to the Java platform.
boolean isSpaceChar(char)b Determines whether the specified primitive char value is a white-space character according to the Unicode specification.
boolean isJavaIdentifierStart(char)c
boolean isJavaIdentifierPart(char)d
Determines whether the specified primitive char value can be the first character in a legal identifier or be a part of a legal identifier, respectively.
a. Added to the Java platform for the 1.1 release. Replaces isSpace(char), which is deprecated.
b. Added to the Java platform for the 1.1 release.
c. Added to the Java platform for the 1.1 release. Replaces isJavaLetter(char), which is deprecated.
d. Added to the Java platform for the 1.1 release. Replaces isJavaLetterOrDigit(char), which is deprecated.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.