home > rapid-tutorial > java

Check whether a character is Uppercase in Java_

Checking if a character is uppercase or not can be useful in many applications. Lucky for us Java’s Character class provides a static method called isUpperCase() to tackle this problem. We simply provide it with the char value to check and it returns true if it is an uppercase letter and false otherwise.

Example

public class CheckUpperCase {
    public static void main(String[] args) {
        char lower = 'a';
        char upper = 'A';

        if (Character.isUpperCase(lower)) {
            System.out.println(lower + " is an uppercase letter");
        } else {
            System.out.println(lower + " is not an uppercase letter");
        }

        if (Character.isUpperCase(upper)) {
            System.out.println(upper + " is an uppercase letter");
        } else {
            System.out.println(upper + " is not an uppercase letter");
        }
    }
}

The output of the program:

a is not an uppercase letter
A is an uppercase letter

In terms of readability, performance and maintainability this is the best approach but let’s consider using:

  • ASCII codes
  • Regular expressions

Using ASCII codes

As seen in the post What on earth is ASCII we can use ASCII values to determine if a given character is lowercase or uppercase. The code fragment below uses the ASCII values of the uppercase letters, A to Z, to determine whether the character is uppercase:

public class CheckUpperCase {
    public static void main(String[] args) {
        char lower = 'a';
        char upper = 'A';

        if ('A' <= lower &&  lower <= 'Z') {
            System.out.println(lower + " is an uppercase letter");
        } else {
            System.out.println(lower + " is not an uppercase letter");
        }

        if ('A' <= upper &&  upper <= 'Z') {
            System.out.println(upper + " is an uppercase letter");
        } else {
            System.out.println(upper + " is not an uppercase letter");
        }
    }
}

Using a Regular Expression

When it comes to searching for patterns in text Regular expressions are always the first option that most programmers consider. This might be overkill but why not? The code fragment below uses Java’s util.regex package to determine whether a character is uppercase or not:

import java.util.regex.Pattern;

public class CheckUpperCase {
    public static void main(String[] args) {
        char lower = 'a';
        char upper = 'A';

        if (Pattern.matches("[A-Z]", "" + lower)) {
            System.out.println(lower + " is an uppercase letter");
        } else {
            System.out.println(lower + " is not an uppercase letter");
        }

        if (Pattern.matches("[A-Z]", "" + upper)) {
            System.out.println(upper + " is an uppercase letter");
        } else {
            System.out.println(upper + " is not an uppercase letter");
        }
    }
}

Note: The code uses "" + lower and "" + upper as the matches() method requires a CharSequence not a char of which String is one.

Not bad right? Whether you use isUpperCase() or ASCII values or a regular expression, it all comes down to a simple if statement.

table of contents

Ready to join the movement?

Dive into our articles, explore our resources, and join the clan. Together, let's rewrite the rules of coding education and prove that learning can be fun, effective, and a little bit rebellious.

Subscribe to receive updates about new posts.