Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Character isDefined(char ch) Method

Java isDefined(char ch) method is a part of Character class. This method is used to check whether the specified character is defined in Unicode or not. For a character to be defined in Unicode, a character must satisfy either of the two(or both) conditions:

  • The Character must have an entry in the UnicodeData file.
  • The value of Character is in a range defined by the UnicodeData file.

Syntax:

public static boolean isDefined(char ch)  

Parameter:

The parameter passed is the character value to be checked whether it is defined in Unicode or not.

Returns:

Returns the boolean value true if the specified character is defined in Unicode else return false.

Example 1:

Here, the characters are checked whether they are defined in Unicode or not.

public class StudyTonight 
{
	public static void main(String[] args) 
	{
		char cp1 = 'A';
		char cp2 = '0';
		char cp3 = '*';
		char cp4 = '%';
		char cp5 = '^';

		boolean b1 = Character.isDefined(cp1);
		boolean b2 = Character.isDefined(cp2);
		boolean b3 = Character.isDefined(cp3);
		boolean b4 = Character.isDefined(cp4);
		boolean b5 = Character.isDefined(cp5);

		System.out.println(cp1 + " is defined?? :  " + b1);
		System.out.println(cp2 + " is defined?? :  " + b2);
		System.out.println(cp3 + " is defined?? :  " + b3);
		System.out.println(cp4 + " is defined?? :  " + b4);
		System.out.println(cp5 + " is defined?? :  " + b5);
	}
}


A is defined?? : true
0 is defined?? : true
* is defined?? : true
% is defined?? : true
^ is defined?? : true

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.util.Scanner;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		try 
		{
			System.out.print("Enter the character: ");
			Scanner sc = new Scanner(System. in );
			char ch = sc.next().charAt(0);
			boolean b = Character.isDefined(ch);
			System.out.println(ch + " is defined?? : " + b);
		}
		catch(Exception e) 
		{
			System.out.println("Invalid Input!!");
		}
	}
}


Enter the character: -
- is defined : true
*****************************
Enter the character: 7
7 is defined?? : true

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.