Checking if a character is lowercase or not can be useful in many applications. Lucky for us Java’s Character
class provides a static method called isLowerCase()
to tackle this problem. We simply provide it with the char
value to check and it returns true
if it is a lowercase letter and false
otherwise.
Example
public class CheckLowerCase {
public static void main(String[] args) {
char lower = 'a';
char upper = 'A';
if (Character.isLowerCase(lower)) {
System.out.println(lower + " is a lowercase letter");
} else {
System.out.println(lower + " is not a lowercase letter");
}
if (Character.isLowerCase(upper)) {
System.out.println(upper + " is a lowercase letter");
} else {
System.out.println(upper + " is not a lowercase letter");
}
}
}

The output of the program:
a is a lowercase letter A is not a lowercase 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 lowercase letters, a to z, to determine whether the character is lowercase:
public class CheckLowerCase {
public static void main(String[] args) {
char lower = 'a';
char upper = 'A';
if ('a' <= lower && lower <= 'z') {
System.out.println(lower + " is a lowercase letter");
} else {
System.out.println(lower + " is not a lowercase letter");
}
if ('a' <= upper && upper <= 'z') {
System.out.println(upper + " is a lowercase letter");
} else {
System.out.println(upper + " is not a lowercase 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 lowercase or not:
import java.util.regex.Pattern;
public class CheckLowerCase {
public static void main(String[] args) {
char lower = 'a';
char upper = 'A';
if (Pattern.matches("[a-z]", "" + lower)) {
System.out.println(lower + " is a lowercase letter");
} else {
System.out.println(lower + " is not a lowercase letter");
}
if (Pattern.matches("[a-z]", "" + upper)) {
System.out.println(upper + " is a lowercase letter");
} else {
System.out.println(upper + " is not a lowercase 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 isLowerCase()
or ASCII values or a regular expression, it all comes down to a simple if
statement.