In the previous example How do I know if a character is lowercase?, we’ve learned how to use the Character.isLowerCase()
method. Now we will learn how to use the other method, which is the Character.isUpperCase()
to determine if a letter is in an uppercase form. This method also return a boolean value that indicates whether the character is in uppercase form or not.
Another way to check if a letter is in uppercase form is by comparing the type of the characters, that can be obtained using the Character.getType()
method with a defined constant value Character.UPPERCASE_LETTER
.
Below is the code snippet that demonstrate this methods.
package org.kodejava.example.lang;
public class CharacterIsUperCaseExample {
public static void main(String[] args) {
char a = 'A';
// Checks to see if a letter is a uppercase letter.
if (Character.isUpperCase(a)) {
System.out.println(a + " is an uppercase character.");
}
// Checks to see if a letter is an uppercase letter
// by comparing the character type against
// Character.UPPERCASE_LETTER constant value.
int charType = Character.getType(a);
if (charType == Character.UPPERCASE_LETTER) {
System.out.println(a + " is an uppercase character.");
}
}
}
The above example give you the following result when it executed:
A is an uppercase character.
A is an uppercase character.