Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The Java platform contains three classes that you can use when working with character data:
- Character--A class whose instances can hold a single character value. This class also defines handy methods that can manipulate or inspect single-character data.
- String-- A class for working with immutable (unchanging) data composed of multiple characters.
- StringBuffer--A class for storing and manipulating mutable data composed of multiple characters.
An object ofCharacter
type contains a single character value. You use aCharacter
object instead of a primitivechar
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 a vector, that requires objects. The following sample program,CharacterDemo
, creates a few character objects and displays some information about them. The code that is related to theCharacter
class is shown in boldface:The following is the output from this program: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."); } }Thea is less than b. a is equal to a2. The character a is lowercase.CharacterDemo
program calls the following constructors and methods provided by theCharacter
class:
Character(char)
- The
Character
class's only constructor, which creates aCharacter
object containing the value provided by the argument. Once aCharacter
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 manyCharacter
class methods that inspect or manipulate character data.This section covers these string-related topics:
Note to C and C++ Programmers: Java strings are first-class objects, unlike C and C++ strings, which are simply null-terminated arrays of 8-bit characters.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.