New Tutorials:   NUMPY    TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL    Matplotlib    C++ Programs
See the Tutorial List

Java Character getType(char ch) Method

Java getType(char ch) method is a part of Character class. This method returns the value that indicates the general category of the specified char value.

It must be noted that this method is not suitable for supplementary characters. For supporting the supplementary characters the method getType(int codePoint) is used.

Syntax:

public static int getType(char ch)  

Parameters:

The parameter passed is the character whose value of the general category will be returned.

Returns:

Returns the integer value representing the general category of the character.

Example 1:

Here, the integer values representing the general category of the characters are returned by using the getType() method.

public class StudyTonight 
{
	public static void main(String[] args) 
	{
		char ch1 = 'A';
		char ch2 = '*';
		char ch3 = '7';
		char ch4 = '#';
		char ch5 = 'b';
		int n1 = Character.getType(ch1);
		int n2 = Character.getType(ch2);
		int n3 = Character.getType(ch3);
		int n4 = Character.getType(ch4);
		int n5 = Character.getType(ch5);
		System.out.println("The value of " + ch1 + " is : " + n1);
		System.out.println("The value of " + ch2 + " is : " + n2);
		System.out.println("The value of " + ch3 + " is : " + n3);
		System.out.println("The value of " + ch4 + " is : " + n4);
		System.out.println("The value of " + ch5 + " is : " + n5);
	}
}


The value of A is : 1
The value of * is : 24
The value of 7 is : 9
The value of # is : 24
The value of b is : 2

Example 2:

Here is a general example where the user can enter the input of his choice and get the desired output.

import java.util.Scanner;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		try 
		{
			System.out.println("Enter the character");
			Scanner sc = new Scanner(System. in );
			char ch = sc.next().charAt(0);
			int r = Character.getType(ch);
			System.out.println("The character " + ch + " has type value : " + r);
		}
		catch(Exception e) 
		{
			System.out.println("Invalid input");
		}
	}
}


Enter the character
y
The character 121 has type value : 2
********************************************
Enter the character
(
The character 40 has type value : 21

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.