Java Character Methods

In this unit, we will learn some of the most commonly used character methods that are useful for performing operations on characters.

1. Character Methods

Similar to the String class, which provides methods to perform operations on strings, the Character class provides methods to perform operations on characters (char values).

Here are some commonly used character methods:

  • isLetter()

  • isDigit()

  • isWhitespace()

  • isUpperCase()

  • isLowerCase()

  • toUpperCase()

  • toLowerCase()

  • toString()

1.1 isLetter()

The isLetter() method checks whether the given character is an alphabet and returns a boolean value true if it is, otherwise false

Syntax

Character.isLetter(value);

 

 

Example 

class Main {

    public static void main(String[] args) {

        char ch1 = 'a';

        char ch2 = '4';

        System.out.println(Character.isLetter(ch1)); // isLetter method called

        System.out.println(Character.isLetter(ch2)); // isLetter method called

    }

}

 

Output

true

false

 

1.2 isDigit()

The isDigit() method checks whether the given character is a number/digit and returns a boolean value true if it is, otherwise false

Syntax

Character.isDigit(value);

 

Example

class Main {

    public static void main(String[] args) {

        char ch1 = 'a';

        char ch2 = '4';

        System.out.println(Character.isDigit(ch1)); // isDigit method called

        System.out.println(Character.isDigit(ch2)); // isDigit method called

    }

}

 

Output

false

true

1.3 isWhitespace()

The isWhitespace() method checks whether the given character is a whitespace and returns a boolean value true if it is, otherwise false

Syntax

Character.isWhitespace(value);

 

 

Example

class Main {

    public static void main(String[] args) {

        char ch1 = ' ';

        char ch2 = 'a';

        System.out.println(Character.isWhitespace(ch1)); // isWhitespace method called

        System.out.println(Character.isWhitespace(ch2)); // isWhitespace method called

    }

}

 

Output

true

false

1.4 isUpperCase()

The isUpperCase() method checks whether the given character is an uppercase letter and returns a boolean value true if it is, otherwise false

Syntax

Character.isUpperCase(value);

 

 

Example

class Main {

    public static void main(String[] args) {

        char ch1 = 'A';

        char ch2 = 'a';

        System.out.println(Character.isUpperCase(ch1)); // isUpperCase method called

        System.out.println(Character.isUpperCase(ch2)); // isUpperCase method called

    }

}

 

Output

true

false

1.5 isLowerCase()

The isLowerCase() method checks whether the given character is a lowercase letter and returns a boolean value true if it is, otherwise false

Syntax

Character.isLowerCase(value);

 

 

Example

class Main {

    public static void main(String[] args) {

        char ch1 = 'a';

        char ch2 = 'A';

        System.out.println(Character.isLowerCase(ch1)); // isLowerCase method called

        System.out.println(Character.isLowerCase(ch2)); // isLowerCase method called

    }

}

 

Output

true

false

 

1.6 toUpperCase()

The toUpperCase() method converts the given lowercase letter to uppercase and returns the new character.

All characters other than lowercase remain unchanged.

Syntax

Character.toUpperCase(value);

 

Example

class Main {

    public static void main(String[] args) {

        char ch1 = 'a';

        char ch2 = 'A';

        char ch3 = '*';

        System.out.println(Character.toUpperCase(ch1)); // toUpperCase method called

        System.out.println(Character.toUpperCase(ch2)); // toUpperCase method called

        System.out.println(Character.toUpperCase(ch3)); // toUpperCase method called

    }

}

 

Output

A

A

*

As we can see, in the above output, ch2 and ch3 remain unchanged.

 

1.7 toLowerCase()

The toLowerCase() method converts the uppercase letter to lowercase and returns the new character.

All characters other than uppercase remain unchanged.

Syntax

Character.toLowerCase(value);

 

Example

class Main {

    public static void main(String[] args) {

        char ch1 = 'A';

        char ch2 = 'a';

        char ch3 = '*';

        System.out.println(Character.toLowerCase(ch1)); // toLowerCase method called

        System.out.println(Character.toLowerCase(ch2)); // toLowerCase method called

        System.out.println(Character.toLowerCase(ch3)); // toLowerCase method called

    }

}

 

Output

a

a

*

As we can see, in the above output, ch2 and ch3 remain unchanged.

 

1.8 toString()

The toString() method converts the character into the respective string. The method returns a String object representing the specified character value that is, a one-character string.

Syntax

Character.toString(value);

Let's check the working of the toString() with an example by getting its data type.

 

Example

class Main {

    public static void main(String[] args) {

        char ch1 = 'A';

        System.out.println(Character.toString(ch1));

        System.out.println((Character.toString(ch1)).getClass().getSimpleName());

    }

}

 

Output

A

String

In the above code, we can see that the character method returns a String value.

 

Possible Mistakes

Passing string instead of a character

The character methods only accept char data type as input.

Example

class Main {

    public static void main(String[] args) {

        String str = "*";

 

        // String passed to character method

        System.out.println(Character.toUpperCase(str));

    }

}

 

Output

file.java:6: error: no suitable method found for toUpperCase(String)

      System.out.println(Character.toUpperCase(str));

                           ^

   method Character.toUpperCase(char) is not applicable

    (argument mismatch; String cannot be converted to char)

   method Character.toUpperCase(int) is not applicable

    (argument mismatch; String cannot be converted to int)

1 error

 

Conclusion

  • Here are the character methods we have learned

    • isLetter(): Checks whether the character is a letter.

    • isDigit(): Checks whether the character is a number.

    • isWhitespace(): Checks whether the character is a whitespace.

    • isUpperCase(): Checks whether the character is an uppercase letter.

    • isLowerCase(): Checks whether the character is a lowercase letter.

    • toUpperCase(): Converts the given character into uppercase letter.

    • toLowerCase(): Converts the given character into lowercase letter.

    • toString(): Converts the given character into a single character string representing the character