Java - Character getType() Method



The Java Character getType() method is used to get a value indicating a character's general category. As we know, each code point has a value assigned to a General Category in Unicode.

For example, all the uppercase letters belong to the UPPERCASE category; similarly, all the lowercase letters belong to the LOWERCASE category. Symbols have various sub-categories like CURRENCY_SYMBOLS, MATH_SYMBOL, etc.

However, one polymorphic form of this method (that takes a char value as arguments) does not work for supplementary characters. To get the value of general category of supplementary characters, we can use the polymorphic form that takes codePoint as arguments.

Syntax

Following is the syntax for Java Character getType() method

public static int getType(char ch)
(or)
public static int getType(int codePoint)

Parameters

  • ch − the character to be tested

  • codePoint − the codepoint to be tested

Return Value

This method returns a value representing the character's general category. It is of return type int.

Example

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

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

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

      // assign values to ch1, ch2
      ch1 = 'M';
      ch2 = '$';

      // create 2 int primitives i1, i2
      int i1, i2;

      // assign getType values of ch1, ch2 to i1, i2
      i1 = Character.getType(ch1);
      i2 = Character.getType(ch2);

      /**
       *  value 1 represents UPPERCASE_LETTER
       *  value 26 represents CURRENCY_SYMBOL
      */

      String str1 = "Category of " + ch1 + " is " + i1;
      String str2 = "Category of " + ch2 + " is " + i2;

      // print i1, i2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

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

Category of M is 1 
Category of $ is 26

Example

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

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {
      int cp1, cp2;
      cp1 = 0x0034;
      cp2 = 0x006f;
      int i1, i2;
      i1 = Character.getType(cp1);
      i2 = Character.getType(cp2);

      /**
       *  value 9 represents DECIMAL_DIGIT_NUMBER
       *  value 2 represents LOWERCASE_LETTER
      */
      String str1 = "Category of cp1 is " + i1;
      String str2 = "Category of cp2 is " + i2;
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

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

Category of cp1 is 9
Category of cp2 is 2

Example

In another example below, we will display the general categories of case-based letters in Unicode System. The method returns 1 for uppercase letters and 2 for lowercase letters.

import java.lang.*;
public class getTypeDemo {
   public static void main(String[] args) {
      char ch1, ch2;
      ch1 = 'a';
      ch2 = 'A';
      int i1, i2;
      i1 = Character.getType(ch1);
      i2 = Character.getType(ch2);
      System.out.println("Category of " + ch1 + " is " + i1);
      System.out.println("Category of " + ch2 + " is " + i2);
   }
}

Output

If we compile and run the program above, the output is displayed as −

Category of a is 2
Category of A is 1

Example

There are sub-categories in symbols too. Punctuation symbols differ based on their usage. Let us see the categories of two different types of dash symbols using this method.

import java.lang.*;
public class getTypeDemo {
   public static void main(String[] args) {
      char ch1, ch2;
      ch1 = '-';
      ch2 = '_';
      System.out.println("Category of " + ch1 + " is " + Character.getType(ch1));
      System.out.println("Category of " + ch2 + " is " + Character.getType(ch2));
   }
}

Output

If we compile the code and run it, the output will be obtained as −

Category of - is 20
Category of _ is 23
Advertisements