Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
An object of Character 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 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
, 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."); } }In the above example, the codea is less than b. a is equal to a2. The character a is lowercase.Character.isUpperCase(a.charValue())
extracts thechar
value from theCharacter
object nameda
. This is because theisUpperCase
method accepts a parameter of typechar
. If you are using JDK 5.0 or later, you can take advantage of unboxing by simply passing this method theCharacter
object:TheCharacter.isUpperCase(a)CharacterDemo
program calls the following constructors and methods provided by theCharacter
class: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 API specification.
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 many
Character
class methods that inspect or manipulate character data.
a. Added to the Java platform for the 1.1 release. Replaces isSpace(char), which is deprecated.
Useful Class Methods in the Character
ClassMethod 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)dDetermines whether the specified primitive char value can be the first character in a legal identifier or be a part of a legal identifier, respectively.
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.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.