char
type. For example:
char ch = 'a'; char uniChar = '\u039A'; // Unicode for uppercase Greek omega character char[] charArray ={ 'a', 'b', 'c', 'd', 'e' }; // an array of chars
char
in a Character
object for this purpose. An object of type Character
contains a single field,
whose type is char
.
This
Character
class also offers a number of useful class (i.e., static) methods for manipulating characters.
You can create a Character
object with the Character
constructor:
Character ch = new Character('a');
Character
object for you under some circumstances.
For example, if you pass a primitive char
into a
method that expects an object,
the compiler automatically converts the char
to a Character
for you.
This feature is called autoboxing—or unboxing, if the conversion
goes the other way.
Here is an example of boxing,
Character ch = 'a'; // the primitive char 'a' is boxed into the Character object ch
Character 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'
Character
class is
immutable, so that once it is created, a Character
object cannot be changed.
Character
class, but is not exhaustive.
For a complete listing of all methods in this class (there are more than 50), refer to the
java.lang.Character
API specification.
Method | Description |
---|---|
boolean isLetter(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) |
Determines whether the specified char value is uppercase or lowercase, respectively. |
char toUpperCase(char ch) |
Returns the uppercase or lowercase form of the specified char value. |
toString(char ch) |
Returns a String object representing the specified character
value—that is, a one-character string. |
Escape Sequence | Description |
---|---|
\t |
Insert a tab in the text at this point. |
\b |
Insert a backspace in the text at this point. |
\n |
Insert a newline in the text at this point. |
\r |
Insert a carriage return in the text at this point. |
\f |
Insert 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
She said "Hello!" to me.
System.out.println("She said \"Hello!\" to me.");