Java - Character isUpperCase() Method



The Java Character isUpperCase() method determines if a character is an uppercase character or not.

A character is said to be an uppercase character if its general category type is UPPERCASE_LETTER as provided by Character.getType(ch) method; or it has contributory property Other_Uppercase as defined by the Unicode Standards.

As per the generalized definition, in the uppercase written style, the characters are all present in their capital forms. For example, a character formed by combining basic Latin letters, "LJ" is in its uppercase style.

Syntax

Following is the syntax for Java Character isUpperCase() method

public static boolean isUpperCase(char ch)
(or)
public static boolean isUpperCase(int codePoint)

Parameters

  • ch − the character to be tested

  • codePoint − the Unicode code point to be tested

Return Value

This method returns true if the argument is uppercased, false otherwise.

Example

The following example shows the usage of Java Character isUpperCase(char ch) method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 char primitives ch1, ch2
      char ch1, ch2;

      // assign values to ch1, ch2
      ch1 = 'K';
      ch2 = '\u0e16'; // represents THAI CHARACTER THO THUNG

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      // check if ch1, ch2 are uppercase and assign results to b1, b2
      b1 = Character.isUpperCase(ch1);
      b2 = Character.isUpperCase(ch2);
      String str1 = ch1 + " is uppercase character is " + b1;
      String str2 = "ch2 is uppercase character is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

K is uppercase character is true
ch2 is uppercase character is false

Example

The following example shows the usage of Java Character isUpperCase(int codePoint) method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 int primitives cp1, cp2
      int cp1, cp2;

      // assign values to cp1, cp2
      cp1 = 0x004a; // represents J
      cp2 = 0x0071; // represents x

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      // check if cp1, cp2 are uppercase and assign results to b1, b2
      b1 = Character.isUpperCase(cp1);
      b2 = Character.isUpperCase(cp2);
      String str1 = "cp1 is uppercase character is " + b1;
      String str2 = "cp2 is uppercase character is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

cp1 is uppercase character is true
cp2 is uppercase character is false

Example

We can also use loop statements on a character sequence to traverse through it and check how many uppercase characters are present in it using this method. If there are no uppercase letters present in the string, false is printed as the output.

import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {
	   String s = "HeLLo WorLD";
	   int count = 0;
	   for(int i = 0; i < s.length(); i++) {
	      char ch = s.charAt(i);
	      if(Character.isUpperCase(ch)) {
	         System.out.println(ch + " is an Uppercase Letter");
	         count = count + 1;
	      }
      }
	   if(count == 0)
		   System.out.println("No Uppercase Letters in the String");
   }
}

Output

The output of the program above will be displayed after compiling and running it as follows −

H is an Uppercase Letter
L is an Uppercase Letter
L is an Uppercase Letter
W is an Uppercase Letter
L is an Uppercase Letter
D is an Uppercase Letter

Example

Another example makes use of loop statements to visit every character element in a character array and determine whether it is an uppercase letter.

import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {
	   char c[] = {'a', 'B', 'D', 'f', 'r'};
	   int count = 0;
	   for(int i = 0; i < c.length; i++) {
	      if(Character.isUpperCase(c[i])) {
	         System.out.println(c[i] + " is an Uppercase Letter");
	         count = count + 1;
	      }
	      else {
	         System.out.println(c[i] + " is not an Uppercase Letter");
	      }
	   }
	   if(count == 0)
		   System.out.println("\n\nNo Uppercase Letters in the String");
   }
}

Output

Let us compile and run the program given above, which will produce the output as follows −

a is not an Uppercase Letter
B is an Uppercase Letter
D is an Uppercase Letter
f is not an Uppercase Letter
r is not an Uppercase Letter
java_lang_character.htm
Advertisements