Signup/Sign In

Java Character getName() Method

Java getName() method is a part of Character class. This method returns the assigned Unicode name of the character at the specified codepoint or null is the specified codepoint is unassigned. If there is no name assigned to the character by the Unicode Data files then the returned name is the same as the result of the expression.

Syntax:

public static String getName(int codePoint)

Parameter:

The parameter passed is the int codepoint which is the Unicode codePoint of the character.

Returns:

Returns the Unicode name of the character or null if the codepoint is unassigned.

Example 1:

Here, the name assigned to the characters is returned by using the getName() method.

public class StudyTonight 
{  
    public static void main(String[] args)
    {         
      int cp1 = 99;  
      int cp2 = 22;  
      int cp3 = 00;  
      int cp4 = 55;  
      int cp5 = 112;       
      String r1 = Character.getName(cp1); //return name for the specified character
      String r2 = Character.getName(cp2);  
      String r3 = Character.getName(cp3);  
      String r4 = Character.getName(cp4);
      String r5 = Character.getName(cp5);
      System.out.println("The character " + (char)cp1 + " has name : " + r1);  
      System.out.println("The character " + (char)cp2 + " has name : " + r2);  
      System.out.println("The character " + (char)cp3 + " has name : " + r3);  
      System.out.println("The character " + (char)cp4  + " has name : " + r4);
      System.out.println("The character " + (char)cp5  + " has name : " + r5);
    }
} 


The character c has name : LATIN SMALL LETTER C
The character  has name : SYNCHRONOUS IDLE
The character has name : NULL
The character 7 has name : DIGIT SEVEN
The character p has name : LATIN SMALL LETTER P

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 Unicode codepoint"); 
			Scanner sc = new Scanner(System.in);
			int cp = sc.nextInt();   
			String r = Character.getName(cp);  
			System.out.println("The character " + (char)cp + " has name : " + r);
		}
		catch(Exception e)
		{
			System.out.println("Invalid input");
		} 
	}
} 


Enter the Unicode codepoint
65
The character A has name : LATIN CAPITAL LETTER A
*****************************************************************
Enter the Unicode codepoint
34
The character " has name : QUOTATION MARK
*****************************************************************
Enter the Unicode codepoint
r
Invalid input

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.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.