Java - Character codePointOf() Method



Description

The Java Character codePointOf() method is used to get the code point value of a Unicode character specified by the given character name.

A character name is a unique character name given to each Unicode code point. For example, the character name for A is LATIN CAPITAL LETTER A. These character names are usually defined in the UnicodeData file.

But if the names are not assigned by the UnicodeData file, it would be a result of the following expression −

Character.UnicodeBlock.of(codePoint).toString().replace('_', ' ') + " " + Integer.toHexString(codePoint).toUpperCase(Locale.ROOT);

This method matches the character names case insensitively, and also removes all the trailing and leading whitespace characters.

For all the code points present in the UnicodeData file, this method recognizes the name that is defined in the "Unicode Name Property" section in the Unicode Standard. For other code points, this method recognizes the name generated using the getName(int) method.

Syntax

Following is the syntax of the Java Character codePointOf() method

public static int codePointOf(String name)

Parameters

  • name − The character name

Return Value

This method returns the Unicode code point of the character represented by the given character name.

Getting Unicode Code Point of a valid String Example

The following example shows the usage of the Java Character codePointOf() method. We've initialized a String variable with a valid unicode character name and then using codePointOf() method, we're getting code point of the string and result is printed.


public class codePointOf{    
   public static void main(String [] args) {             
      String s = "latin capital letter a";        
      int cp;        
      cp = Character.codePointOf(s);        
      System.out.println("The code point value of the given character name: " + cp);
   }   
}

Output

If we compile and run the given program, the output will be produced as follows −

The code point value of the given character name: 65

Facing Exception while Getting Unicode Code Point of an invalid String Example

But if the given character name argument passed to the method is unrecognized or if it is not a valid character name, an IllegalArgument Exception is thrown.


public class codePointOf{    
   public static void main(String [] args){             
      String s = "small letter a"; // it is not a valid character name        
      int cp; // code point value returned by the method is stored in this variable        
      cp = Character.codePointOf(s);
        
      //print the return value
      System.out.println("The code point value of the given character name: " + cp);
   }   
}

Exception

When the program is compiled and run, the method throws an exception as follows −

Exception in thread "main" java.lang.IllegalArgumentException: Unrecognized character name :SMALL LETTER A
        at java.base/java.lang.Character.codePointOf(Character.java:11674)
        at codePointOf.main(codePointOf.java:11)

Facing Exception while Getting Unicode Code Point of a null String Example

In this example, let us see a scenario where the string passed as an argument to the method is initialized as null. The method will throw an NullPointer Exception.


public class codePointOf{    
   public static void main(String [] args){             
      String s = null; // null string        
      int cp; // code point value returned by the method is stored in this variable        
      cp = Character.codePointOf(s);
        
      //print the return value
      System.out.println("The code point value of the given character name: " + cp);
   }   
}

Exception

If we compile and run the program above, the exception is thrown as follows −

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.trim()" because "name" is null
        at java.base/java.lang.Character.codePointOf(Character.java:11662)
        at codePointOf.main(codePointOf.java:11)
java_lang_character.htm
Advertisements