Most of the time, if you are using a single character value, you will use the primitivechartype. For example:There are times, however, when you need to use a char as an object—for example, as a method argument where an object is expected. The Java programming language provides a wrapper class that "wraps" thechar ch = 'a'; char uniChar = '\u039A'; // Unicode for uppercase Greek omega character char[] charArray ={ 'a', 'b', 'c', 'd', 'e' }; // an array of charscharin aCharacterobject for this purpose. An object of typeCharactercontains a single field, whose type ischar. ThisCharacterclass also offers a number of useful class (i.e., static) methods for manipulating characters.You can create a
Characterobject with theCharacterconstructor:The Java compiler will also create aCharacter ch = new Character('a');Characterobject for you under some circumstances. For example, if you pass a primitivecharinto a method that expects an object, the compiler automatically converts thecharto aCharacterfor you. This feature is called autoboxing—or unboxing, if the conversion goes the other way.Here is an example of boxing,
and here is an example of both boxing and unboxing,Character ch = 'a'; // the primitive char 'a' is boxed into the Character object chCharacter test(Character c) {...} // method parameter and return type = Character object char c = test('x'); // primitive 'x' is boxed for method test, return is unboxed to char 'c'The following table lists some of the most useful methods in the
Note: TheCharacterclass is immutable, so that once it is created, aCharacterobject cannot be changed.Characterclass, but is not exhaustive. For a complete listing of all methods in this class (there are more than 50), refer to thejava.lang.CharacterAPI specification.
Useful Methods in the CharacterClassMethod 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. boolean isUpperCase(char ch)
boolean isLowerCase(char ch)Determines whether the specified char value is uppercase or lowercase, respectively. char toUpperCase(char ch)
char toLowerCase(char ch)Returns the uppercase or lowercase form of the specified char value. toString(char ch)Returns a Stringobject representing the specified character value—that is, a one-character string.Escape Sequences
A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. The following table shows the Java escape sequences:
Escape Sequences Escape Sequence Description \tInsert a tab in the text at this point. \bInsert a backspace in the text at this point. \nInsert a newline in the text at this point. \rInsert a carriage return in the text at this point. \fInsert a formfeed in the text at this point. \'Insert a single quote character in the text at this point. \"Insert a double quote character in the text at this point. \\Insert a backslash character in the text at this point. When an escape sequence is encountered in a print statement, the compiler interprets it accordingly. For example, if you want to put quotes within quotes you must use the escape sequence, \", on the interior quotes. To print the sentence
you would writeShe said "Hello!" to me.System.out.println("She said \"Hello!\" to me.");