Character
class wraps a value of the primitive type
Java Character in an object. An object of type
Character
contains a single field whose type is
char
.
In addition, this class provides several methods for determining a character's category (lowercase letter, digit etc.,) and for converting characters from uppercase to lowercase and vice versa.
Examples :
char ch = 'a'; // Char
char uniChar = '\u039A'; // Unicode for char
char[] charArray = { 'a', 'b', 'c', 'd', 'e' }; // Array of chars
Character Methods :
Escape Sequences :
Character
class CharacterTest
{
public static void main(String arg[])
{
System.out.println("Checks for letter : " + Character.isLetter('M'));
System.out.println("Checks for digit : " + Character.isDigit('5'));
System.out.println("Checks for white space : " + Character.isWhitespace(' '));
System.out.println("Checks for uppercase : " + Character.isUpperCase('C'));
System.out.println("Checks for lowercase : " + Character.isLowerCase('c'));
System.out.println("Converts to uppercase : " + Character.toUpperCase('m'));
System.out.println("Converts to lowercase : " + Character.toLowerCase('Z'));
System.out.println("Converts to string : " + Character.toString('w'));
}
}
OUTPUTChecks for letter : true
Checks for digit : true
Checks for white space : true
Checks for uppercase : true
Checks for lowercase : true
Converts to uppercase : M
Converts to lowercase : z
Converts to string : w
DESCRIPTIONIn this program, all the character methods are used.
THINGS TO TRY
- Give
'8'
in isLetter
method and see the output which returns false
.
- Give
'k'
in isDigit
method and see the output which returns false
.
- Give
'\t', '\n'
in isWhitespace
method and see the output which returns true
as newline and tab contains space.
- Take a character and replace in
isUppercase, isLowercase, toUppercase, toLowercase
and toString
methods.