Characters in Java

Below example will illustrate various ways in which we can create a character and perform operations on it.

package strings;

public class CharacterClass {

    public static void main(String [] args){

        char c1 = 'a';

        //check if c1 is lower case
        System.out.println("Is c1 lower case -? -> " + Character.isLowerCase(c1));

        //check if c1 is upper case
        System.out.println("Is c1 upper case -? -> " + Character.isUpperCase(c1));

        //check if c1 is a white space
        System.out.println("Is c1 a white space ? -> " + Character.isWhitespace(c1));

        //check if c1 is a digit
        System.out.println("Is c1 digit? -> " + Character.isDigit(c1));

        //check if c1 is a letter
        Character c2 = '$';
        System.out.println("Is c2($) letter? -> " + Character.isLetter(c2));

        //check if c1 is a alphabetic
        System.out.println("Is c2($) alphabetic? -> " + Character.isAlphabetic(c2));

    }
}

Here is the output of above program.

Is c1 lower case -? -> true
Is c1 upper case -? -> false
Is c1 a white space ? -> false
Is c1 digit? -> false
Is c2($) letter? -> false
Is c2($) alphabetic? -> false

You may also like...