Character getType() Method in Java with examples

  1. The Character.getType(char ch) is an inbuilt method in java that returns a value indicating a character’s general category. This method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the getType(int) method.

    Syntax:

    public static int getType(char ch)

    Parameters: The method accepts one parameter ch of character datatype which refers to the character to be tested.



    Return value: This method returns a value of type integer representing the character’s general category.

    Below programs illustrates the use of Character.getType(char ch) method:
    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import java.lang.*;
      
    public class gfg {
      
       public static void main(String[] args) {
      
      
          // Create 2 character primitives ch1, ch2 and assigning values
          char c1 = 'K', c2 = '%';
      
          // Assign getType values of c1, c2 to int primitives int1, int2
          int int1 = Character.getType(c1);
          int int2 = Character.getType(c2);
      
          String str1 = "Category of " + c1 + " is " + int1;
          String str2 = "Category of " + c2 + " is " + int2;
      
          System.out.println( str1 );
          System.out.println( str2 );
       }
    }

    chevron_right

    
    

    Output:

    Category of K is 1
    Category of % is 24
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import java.lang.*;
      
    public class gfg {
      
       public static void main(String[] args) {
      
      
          // Create 2 character primitives ch1, ch2 and assigning values
          char c1 = 'T', c2 = '^';
      
          // Assign getType values of c1, c2 to inyt primitives int1, int2
         int int1 = Character.getType(c1);
         int int2 = Character.getType(c2);
      
          String str1 = "Category of " + c1 + " is " + int1;
          String str2 = "Category of " + c2 + " is " + int2;
      
          System.out.println(str1);
          System.out.println(str2);
       }
    }

    chevron_right

    
    

    Output:

    Category of T is 1
    Category of ^ is 27
    
  2. The java.lang.Character getType(int codePoint) is similar to previous method in all the manner, this method can handle supplementary characters.

    Syntax:

    public static int getType(int codePoint)
    

    Parameter: The method accepts a single parameter codePoint of integer datatype and refers to the character (Unicode code point) to be tested.

    Return Value: This method returns a value of type int representing the character’s general category.

    Below programs demonstrates the above mentioned method:
    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // the above method
    import java.lang.*;
      
    public class gfg {
      
       public static void main(String[] args) {
      
          // int primitives c1, c2
          int c1 = 0x0037, c2 = 0x016f;
      
      
          // Assign getType values of c1, c2 to int primitives int1, int2
         int int1 = Character.getType(c1);
         int int2 = Character.getType(c2);
      
          // Print int1, int2 values
          System.out.println(  "Category of c1 is " + int1);
          System.out.println(  "Category of c1 is " + int2);
       }
    }

    chevron_right

    
    

    Output:

    Category of c1 is 9
    Category of c1 is 2
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate
    // the above method
    import java.lang.*;
      
    public class gfg {
      
       public static void main(String[] args) {
      
          // int primitives c1, c2
          int c1 = 0x0135, c2 = 0x015f;
      
      
          // Assign getType values of c1, c2 to int primitives int1, int2
         int int1 = Character.getType(c1);
         int int2 = Character.getType(c2);
      
          // Print int1, int2 values
          System.out.println(  "Category of c1 is " + int1);
          System.out.println(  "Category of c1 is " + int2);
       }
    }

    chevron_right

    
    

    Output:

    Category of c1 is 2
    Category of c1 is 2
    
  3. Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#getType(char)



    My Personal Notes arrow_drop_up

    Contented with little yet wishing for more

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.