Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Most of the time, if you are using a single character value, you will use the primitivechar
type, for example:Thechar ch = 'a'; char[] firstFiveLetters={ 'a', 'b', 'c', 'd', 'e' }; char ch = string.charAt(i);Character
class isn't required for such simple usage, but does offer several useful static methods. Here are some examples:The class is immutable, so once created, a//Test whether a character is upper case. if (Character.isUpperCase(string.charAt(i)) { ... } //Convert a character to lower case. char ch = Character.toLowerCase(string.charAt(i)); //Determine if the character is a letter. if (Character.isLetter(string.charAt(i))) { ... }Character
cannot be changed.If you are passing a primitive data type (such as a
char
) into a method that expects a wrapper object (such as aCharacter
) the Java compiler automatically converts the type for you. This feature is called autoboxing or unboxing if the conversion goes the other way. Autoboxing was added to the compiler in release 5.0, so if you are using an earlier version, you'll have to do the conversion manually.The following tables lists the most useful methods in the
Character
class, but is not exhaustive. For a complete listing of all methods in this class, refer to thejava.lang.Character
API specification.
Useful Methods in the Character
ClassMethod Description boolean isLetter(char ch)
boolean isDigit(char ch)Determines whether the specified char value is a letter or a digit, respectively. boolean isWhiteSpace(char ch) Determines whether the specified char value is white space according to the Java platform. boolean isUpperCase(char ch)
boolean isLowerCase(char ch)Determines whether the specified char value is upper- or lowercase, respectively. char toUpperCase(char ch)
char toLowerCase(char ch)Returns the upper- or lowercase form of the specified char value. toString(char ch) Returns a String
object representing the specified character value.int digit(char ch, int radix) Returns the numeric value of the character in the specified radix.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.