Java Check If Char is Uppercase

Java Check If Char Is Uppercase | To check whether the given character is in upper case or not we have Character.isUpperCase() method. This is present in java.lang package which is the default package available to every Java program hence there is no need to import the Character class.

This method returns a boolean value i.e. if the given character is in uppercase it returns true else it returns false. Now let us see how to check if a char is uppercase in Java.

Example-1:- Java check if character is uppercase.

char ch = 'i';
if(Character.isUpperCase(ch))
   System.out.println("UPPERCASE");
else
   System.out.println("NOT UPPERCASE");

The snippet returns “NOT UPPERCASE” as the given character ‘i’ is in lowercase.

Example 2:- Java check if character is uppercase.

char ch = 'U';
if(Character.isUpperCase(ch))
   System.out.println("UPPERCASE");
else
   System.out.println("NOT UPPERCASE");

The snippet returns “UPPERCASE” as the given character ‘U’ is in uppercase.

Method Syntax:- public boolean Character.isUpperCase(char ch)

  • Parameter: character
  • returns: a boolean value

How to Check if a Char is Uppercase in Java using isUpperCase()

public class Main {
   public static void main(String[] args) {
      char ch = 'L';
      if (Character.isUpperCase(ch)) {
         System.out.println("UPPERCASE");
      } else {
         System.out.println("NOT UPPERCASE");
      }
   }
}

Output:-

UPPERCASE

The below is to show that the given character is not in uppercase.

public class Main {
   public static void main(String[] args) {
      char ch = 'l';
      if (Character.isUpperCase(ch)) {
         System.out.println("UPPERCASE");
      } else {
         System.out.println("NOT UPPERCASE");
      }
   }
}

Output:-

NOT UPPERCASE

In the above code, we need not use toChar() as we are directly using character, here we use the isUpperCase() method in the if loop and return “UPPERCASE” if it is in uppercase, or else we print “NOT UPPERCASE”. 

There are no exceptions to this method. The return type of Character.isUpperCase() method is character. Likewise, we can also check the same thing for the lowercase letter by Character.isLowerCase() this method works the same as Character.isUpperCase(). Also see:- Check If String is Uppercase Java, Check If String Contains Only Uppercase or Lowercase.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *