Menu
Topics Index
...
`


Exploring java.lang > Primitive Type Wrappers >
Siva Nookala - 14 Apr 2016
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 :
MethodDescription
boolean isLetter(char ch)Determines whether the specified char value is a letter.
boolean isDigit(char ch)Determines whether the specified char value is a digit.
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.
boolean isLowerCase(char ch)Determines whether the specified char value is lowercase.
char toUpperCase(char ch)Returns the uppercase form of the specified char value.
char toLowerCase(char ch)Returns the 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.
forDigit(int num, int radix)Returns the digit character associated with the value of num.The radix of the conversion is specified by radix
digit(char digit, int radix)Returns the integer value associated with the specified character according to the specified radix.
isLetterOrDigit()Returns true if given character is a letter or a digit. Otherwise, it returns false.
isIdentifierIgnorable(char ch)Returns true if ch should be ignored in an identifier.Otherwise, it returns false.
isDefined(char ch)Returns true if ch is defined by Unicode.Otherwise, it returns false.

Escape Sequences :
Escape SequenceDescription
\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.

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'));    
    }
}
OUTPUT

Checks 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

DESCRIPTION

In 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.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.